gpt4 book ai didi

c++ - 为什么 sizeof (class with character variable) 在 C++ 中异常?

转载 作者:行者123 更新时间:2023-11-28 07:00:28 25 4
gpt4 key购买 nike

<分区>

考虑以下代码:

#include <iostream>
#include <conio.h>>
using namespace std;

class book
{
char Title;
int no_of_pages;
public:
void read();
void show();
};

int main()
{
book ob;
char Title;
cout << sizeof(book) << " " << sizeof(Title) << endl;

getch();
return 0;
}

代码的输出是

8 1

如果我将类中“标题”的定义更改为以下内容:

char Title[5];

和 main() 的“标题”

char Title[5];

输出变为:

12 5

为了查看是否对程序中的所有字符串变量都执行了此操作,我在 main() 中使用了“Title”。但是,对于在类中声明的字符串,该模式是显而易见的。

为了完成,模式是:

Size of character array is taken to be the least multiple of 4 greater than the actual size of array

问题:虽然我知道它依赖于实现,但有人知道或可以提出 C++ 11 这种行为的原因吗?

我的编译器是 VS 2012,适用于 64 位 Windows。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com