C++中的sizeof

sizeof 返回一个对象或类型名的字节长度。
注意几点:
1、一个string的大小与它所指的字符串的长度无关;
2、应用在指针类型上的sizeof操作符返回的是包含该类型地址所需的内存长度;
3、应用在引用类型上的sizeof操作符返回的是包含被引用对象所需的内存长度。
int *pi = new int[13];
cout << "pi:\t"  << sizeof(pi)  << endl;
cout << "*pi:\t" << sizeof(*pi) << endl;

string st1("fr");
string st2("a submarine");
string *ps = &st1;
cout << "st1:\t" << sizeof(st1) << endl;
cout << "st2:\t" << sizeof(st2) << endl;
cout << "ps:\t"  << sizeof(ps)  << endl;
cout << "*ps:\t" << sizeof(*ps) << endl;

cout << "short:  \t"  << sizeof(short)    << endl;
cout << "short *:\t"  << sizeof(short *)  << endl;
cout << "shout &:\t"  << sizeof(short &)  << endl;
cout << "short[3]:\t" << sizeof(short[3]) << endl;
 
输出:
pi:     4
*pi:    4
st1:    32
st2:    32
ps:     4
*ps:    32
short:          2
short *:        4
shout &:        2
short[3]:       6

原文链接: https://www.cnblogs.com/submarinex/archive/2011/06/09/2077151.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++中的sizeof

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/26943

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午4:33
下一篇 2023年2月8日 上午4:33

相关推荐