Why is address of char data not displayed?

前端 未结 8 1237
执念已碎
执念已碎 2020-11-22 10:02
class Address {
      int i ;
      char b;
      string c;
      public:
           void showMap ( void ) ;
};

void Address :: showMap ( void ) {
            cout          


        
8条回答
  •  -上瘾入骨i
    2020-11-22 10:46

    For the second issue - the compiler by default will pad structure members. The default pad is to the sizeof(int), 4 bytes (on most architectures). This is why an int followed by a char will take 8 bytes in the structure, so the string member is at offset 8.

    To disable padding, use #pragma pack(x), where x is the pad size in bytes.

提交回复
热议问题