Easy forgotten details in c++

Easy forgotten details in c++

Welcome to my blog. Help yourself.
----- perrin

2018-09-27 23:29


int  INT_MAX=pow(2,31)-1=2147483647, the maximum prime within int.

INT_MIN=0X10000000H(complement)=-2147483648, so the sign bit is omitted.

">>",shift operater won't move sign bit. so if you want it works as your first imagine, you should first add unsigned int variable and shift the unsigned int.

for(unsigned int i=0;i<s.size()-1;++i)

this is tricky when s is empty. Because s.size() is unsigned and subtracted by 1 and will underflow to maximun,

when assign values between signed and unsigned values, type convertion will occur.

class EmptyClass{};

class SomeEnd final{}    // can not to be inherited. final is added since c++11. we can use non-virtual destructor and get rid of vptr by making method all non-virtual. and enjoy the pure structure.

class VirtOnly{
    virtual ~VirtOnly(){};
};

char* getMem(){

    char p[]="hello world"; //oops. A array created in stack and p point to stack address.It is reclaimed when return
//const char* p="helloworld";    //this works. as p point to text segment,or more precisely,rodata segment
    return p;

}

void getMem(char** p){
    *p=(char*)malloc(100);
}

int main(int argc,char** argv){
    cout<<sizeof(EmptyClass)<<endl;   //empty class size is 1
    cout<<sizeof(VirtOnly)<<endl;     //the size is the size of a ptr. for the vptr.

    char* str=getMem();
    cout<<str<<endl;
    char* p=NULL;
    getMem(&p);                //it works
    strcpy(p,"helloworld");
    cout<<"p:"<<p<<endl;
}

 

Segment introspection

#include<bits/stdc++.h>
using namespace std;

int main(int argc ,char** argv){
    return 0;
}


perrin@LittleBlack:~/tmp$ size
   text	   data	    bss	    dec	    hex	filename
   2089	    640	      8	   2737	    ab1	a.out



#include<bits/stdc++.h>
using namespace std;

int main(int argc ,char** argv){
    const char *s="h";
    return 0;
}


perrin@LittleBlack:~/tmp$ size
   text	   data	    bss	    dec	    hex	filename
   2107	    640	      8	   2737	    ab1	a.out



So we can know that const literal value lays in text segment.

#include<bits/stdc++.h>
using namespace std;

int gi;

int main(int argc ,char** argv){
    const char *s="h";
    return 0;
}


perrin@LittleBlack:~/tmp$ size
   text	   data	    bss	    dec	    hex	filename
   2107	    640	     16	   2763	    acb	a.out

This tell us that uninitiated global variable lays in bss segment.

#include<bits/stdc++.h>
using namespace std;

int gi;
int gii=0;

int main(int argc ,char** argv){
    const char *s="h";
    return 0;
}


perrin@LittleBlack:~/tmp$ size
   text	   data	    bss	    dec	    hex	filename
   2107	    640	     16	   2763	    acb	a.out


#include<bits/stdc++.h>
using namespace std;

int gi;
int gii=0;
long lii=1;

int main(int argc ,char** argv){
    const char *s="h";
    return 0;
}

perrin@LittleBlack:~/tmp$ size
   text	   data	    bss	    dec	    hex	filename
   2107	    648	     16	   2771	    ad3	a.out

So,we know that initiated global variable lays in data segment, but on 64 bit os, 'int' occupy 4 bytes and aligned with 8 bytes. So the data segment will not be increased by 4 when there are 4 bytes unoccupied space just in time.

 


Commenter Name
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Commenter Name
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Commenter Name
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Search
Page view info
{'ip': '18.204.227.250', 'lat_lon': (39.0481, -77.4728), 'city': 'Ashburn', 'pv': 57919, 'cur_pv': 65, 'cur_daily_pv': 1, 'ur_pv': 4, 'ur_cur_all': 1, 'ur_today_all': 4, 'ur_today_cur': 1, 'daily_pv': 129, 'daily_ip': 80, 'whole_ip': 9791, 'ip_delta': 5}
AMap
Baidu Map