博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
空对象占用的内存空间
阅读量:4695 次
发布时间:2019-06-09

本文共 918 字,大约阅读时间需要 3 分钟。

类中成员变量和成员函数分开存储  1 #include
2 using namespace std; 3 class person 4 { 5 6 }; 7 8 void test() 9 {10 person p;11 // 空对象占一个字节为什么呢?12 // C++为每个空对象分配一个字节的内存空间,是为区分空对象占内存的位置 13 cout << "sizeof of null obj is " << sizeof(p) << endl;14 }15 16 int main()17 {18 test();19 return 0;20 }

1 #include
2 using namespace std; 3 class person 4 { 5 int age;//非静态成员变量属于类对象上的 ,剩下都不是 6 static int va;//静态成员变量不属于类对象上的 7 void func(){ 8 } 9 static void fun(){10 } 11 };12 13 void test()14 {15 person p;16 // 空对象占一个字节为什么呢?17 // C++为每个空对象分配一个字节的内存空间,是为区分空对象占内存的位置 18 cout << "sizeof of null obj is " << sizeof(p) << endl;19 }20 21 void test01()22 {23 person p;24 cout << "sizeof of null obj is " << sizeof(p) << endl;25 } 26 27 int main()28 {29 test01();30 return 0;31 }
View Code

 

转载于:https://www.cnblogs.com/mch5201314/p/11584321.html

你可能感兴趣的文章
客户端第一天学习的相关知识
查看>>
python工具pycharm使用-断点调试
查看>>
Python生成pyc文件
查看>>
Linux防火墙的关闭和开启
查看>>
LeetCode - Same Tree
查看>>
Python dict get items pop update
查看>>
[置顶] 程序员必知(二):位图(bitmap)
查看>>
130242014036-(2)-体验敏捷开发
查看>>
constexpr
查看>>
java web线程池
查看>>
Nginx 流量和连接数限制
查看>>
selenium.common.exceptions.WebDriverException: Message: unknown Error: cannot find Chrome binary
查看>>
iOS - 单例传值 (一)
查看>>
课堂作业1
查看>>
IE8/9 本地预览上传图片
查看>>
Summary of CRM 2011 plug-in
查看>>
Eclipse+Maven环境下java.lang.OutOfMemoryError: PermGen space及其解决方法
查看>>
安全漏洞之Java
查看>>
Oracle 组函数count()
查看>>
Session的使用过程中应注意的一个小问题
查看>>