原创 lightcity 光城 2019-11-02

收录于话题

#C++那些事

53个

string的字节对齐

0.一个结构体大小

现有如下结构体:

struct A {
   int __id;
   long __data;
   string __str;
};

求sizeof(A)=?

1.问题分析

string为basic_string的typedef,对于basic_string我们看到里面:

template<typename _CharT, typename _Traits, typename _Alloc>
class basic_string
{
   struct _Alloc_hider : allocator_type // TODO check __is_final
     {
_Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
: allocator_type(__a), _M_p(__dat) { }

pointer _M_p; // The actual data.
   };

    _Alloc_hider _M_dataplus;
     size_type _M_string_length;

     enum { _S_local_capacity = 15 / sizeof(_CharT) };

     union
     {
       _CharT           _M_local_buf[_S_local_capacity + 1];
       size_type        _M_allocated_capacity;
     };

};

其中size_type的等价于size_t,64位机器上是8字节,指针也是8字节.因此,上述我们可以知道内存结构为

8字节内存指针
8字节字符串长度
匿名的enum,并没有用一个枚举去声明一个名,不占内存.
16字节联合体

故string内部按8字节对齐,共占32字节大小.

2.解析答案

现在回到上面那个问题上:

结构体A的内部结构通过上述的string,我们知道如下:

4字节int
8字节long
32字节string

而32字节又可以被展开为:

8
8
16

根据string我们知道是8字节对齐,据此得出A为8字节对齐.

int+long为12,需要填补到8的倍数边界,故为16,而string为32,不用变,因此最后为16+32=48.

拓展:在一个类中声明一个enum或者结构体,只要没有定义是不占大小的.而union只出现了,就会占大小.例如:

class A{
private:
   class B {
       int a;
       float b;
   };  // 这里不算
   B b;    // 这里算大小

   enum S{ _S_local_capacity = 15 / sizeof(char) };  // 这里不算
   S s; // 这里算大小
   union  // 算大小
   {
       char _M_local_buf[15+ 1];
       int _M_allocated_capacity;
   };
};

ok,本节完!