草庐IT

WPRINTF_SIZEOF

全部标签

c++ - C++ 标准是否保证未使用的私有(private)字段会影响 sizeof?

考虑以下结构:classFoo{inta;};在g++中测试,我得到了sizeof(Foo)==4但标准是否保证了这一点?是否允许编译器注意到a是一个未使用的私有(private)字段并将其从类的内存表示中删除(导致更小的sizeof)?我不希望任何编译器真正进行这种优化,但这个问题出现在语言律师讨论中,所以现在我很好奇。 最佳答案 C++标准没有定义很多关于内存布局的内容。这种情况的基本规则是9Classes部分下的第4项:4Completeobjectsandmembersubobjectsofclasstypeshallhav

c++ - SFINAE + sizeof = 检测表达式是否编译

我刚刚发现如何检查operator为类型提供。templateT&lvalue_of_type();templateTrvalue_of_type();templatestructis_printable{templatestaticchartest(char(*)[sizeof(lvalue_of_type()())]);templatestaticlongtest(...);enum{value=1==sizeoftest(0)};typedefboost::integral_constanttype;};这个技巧是众所周知的,还是我刚刚获得了元编程诺贝尔奖?;)编辑:我使用两个全

c++ - 为什么在使用 libc++ 时 sizeof( std::variant< char > ) == 8 而不是 2 (如 MSVC 的 STL 和 libstdc++)?

考虑thisexampleonCompilerexplorer.基本上,我们有这个代码片段:#include#includeenumclassEnum1:std::uint8_t{A,B};enumclassEnum2:std::uint8_t{C,D};usingVar=std::variant;usingVar2=std::variant;templatestructprint_size;voidfunc(){print_size{};print_size{};}如果我们使用GCC的libstdc++(使用clang或GCC)编译它,我们会得到预期的编译错误:error:impli

c++ - 是否保证 sizeof(std::atomic<integer type>) == sizeof(integer type)?

换句话说,是std::atomic保证只持有一个int值(value)? 最佳答案 没有。根据C++11标准的第29.5/9段:[Note:Therepresentationofanatomicspecializationneednothavethesamesizeasitscorrespondingargumenttype.Specializationsshouldhavethesamesizewheneverpossible,asthisreducestheeffortrequiredtoportexistingcode.—en

c++ - 否定 size_t (即 `-sizeof(struct foo)` ))会发生什么?

我正在处理一些包含表单表达式的代码-(sizeof(structfoo))即size_t的否定,我不清楚C和C++标准对编译器的要求是什么。具体来说,通过查看这里和其他地方,sizeof返回类型为size_t的无符号整数值。在否定无符号整数时,我找不到指定行为的任何明确引用。有没有,如果有,是什么?编辑:好的,所以关于无符号类型的算术有一些很好的答案,但不清楚这实际上是否如此。当这否定时,它是对无符号整数进行操作,还是转换为有符号类型并对其进行处理?从标准中期望的行为是“想象它是相似幅度的负数,然后对无符号值应用'溢出'规则”? 最佳答案

c++ - sizeof如何计算结构的大小

我知道由于对齐,char和int在32位架构上被计算为8个字节,但我最近遇到了一种情况,即sizeof运算符将具有3个短裤的结构报告为6个字节。代码如下:#includeusingnamespacestd;structIntAndChar{inta;unsignedcharb;};structThreeShorts{unsignedshorta;unsignedshortb;unsignedshortc;};intmain(){cout编译器:g++(Debian4.3.2-1.1)4.3.2。这真的让我很困惑,为什么包含3个短裤的结构不强制对齐? 最佳答案

c++ - 为什么引用类型的 sizeof 会给你类型的大小?

根据标准,在[expr.sizeof](5.3.3.2)我们得到:Whenappliedtoareferenceorareferencetype,theresultisthesizeofthereferencedtype.这似乎与未指定引用的事实相一致[dcl.ref](8.3.2.4):Itisunspecifiedwhetherornotareferencerequiresstorage但对我来说,在语言中出现这种不一致似乎很奇怪。不管引用是否需要存储,能够确定引用使用多少大小不是很重要吗?看到这些结果似乎是错误的:sizeof(vector)==24sizeof(vector*)

c++ - c++ sizeof 运算符如何计算大小?

我创建了一个只有2个公共(public)函数(构造函数和析构函数)的类X,并且使用sizeof运算符将类大小变为1。当我在上面的类声明中添加一个char类型的私有(private)数据成员时,大小仍然是1。最后我给它添加了一个整数类型作为类数据成员,现在大小为8字节。请向我解释如何计算类(class)人数。 最佳答案 首先,认识到非虚函数对类的大小没有影响。any类的实例大小至少为1字节,即使类为空,这样不同的对象会有不同的地址。添加char可以确保不同的对象具有不同的地址,因此编译器不会人为地将大小加一。然后大小为sizeof(c

c++ - sizeof 如何知道数组的大小?

这个问题在这里已经有了答案:Howdoessizeofknowthesizeoftheoperandarray?(12个回答)关闭7年前。我的代码如下:main(){intarray[5]={3,6,9,-8,1};printf("thesizeofthearrayis%d\n",sizeof(array));printf("theaddressofarrayis%p\n",array);printf("theaddressofarrayis%p\n",&array);int*x=array;printf("theaddressofxis%p\n",x);printf("thesize

c++ - printf ("%s")、printf ("%ls")、wprintf ("%s") 和 wprintf ("%ls") 之间有什么区别?

考虑这个示例程序:#include#include#includeintmain(){std::stringnarrowstr="narrow";std::wstringwidestr=L"wide";printf("1%s\n",narrowstr.c_str());printf("2%ls\n",widestr.c_str());wprintf(L"3%s\n",narrowstr.c_str());wprintf(L"4%ls\n",widestr.c_str());return0;}这个的输出是:1narrow2wide我想知道:为什么不打印3和4?1&3和2&4有什么区别?如