在下面的代码中,我定义了一个9字节的结构header
,它的大小为16个字节。然后,我从堆中动态分配9个字节,并将其分配给header指针。struct header { uint8_t chunk_id; long int format; }; int main() { std::cout << "sizeof(header) " << sizeof(header) << '\n'; auto head = (header*)malloc(9); head->chunk_id =3; head->format=5; std::cout << (int)head->chunk_id << " " << head->format << '\n' << sizeof(*head); free(head); return 0; }
输出:
仍通知该对象为16字节(虽然为9字节)。我想它只是看struct的定义。但是,当sizeof的大小与实际对象的大小不匹配时,一切如何工作(包括运算符'->')?sizeof(header) 16 3 5 16
事实证明,sizeof
在下面的代码中,我定义了一个9字节的结构头,它的大小为16字节。然后,我从堆中动态分配9个字节,并将其分配给标头指针。 struct ...
但是,当sizeof的大小和实际对象的大小不匹配时,一切如何工作(包括运算符'->'?)>