Tag: sizeof

sizeof(空结构)和sizeof(空数组结构)之间的区别?

我有两个结构定义如下: struct EmptyStruct{ }; struct StructEmptyArr{ int arr[0]; }; int main(void){ printf("sizeof(EmptyStruct) = %ld\n", sizeof(EmptyStruct)); printf("sizeof(StructEmptyArr) = %ld\n", sizeof(StructEmptyArr)); return 0; } 在Ubuntu 14.04,x64上用gcc(g ++)4.8.4编译。 输出(对于gcc和g ++): sizeof(EmptyStruct) = 1 sizeof(StructEmptyArr) = 0 我可以理解为什么sizeof(EmptyStruct)等于1但不明白为什么sizeof(StructEmptyArr)等于0 。 为什么两者之间有差异?

C vs C ++ sizeof

我刚刚遇到这个简单的代码片段,想知道为什么当它由C编译器编译的时候,这个程序的输出是4而当它由C ++编译的时候,是8 。 #include <stdio.h> int x; int main(){ struct x {int a; int b;}; printf("%d", sizeof(x)); return 0; } C ++输出是有理的( 8 = 4 + 4 = sizeof(xa) + sizeof(xb) ),但是C的输出不是。 那么, sizeof如何在C中工作呢? C: https : //ideone.com/zj5Qd2 C ++: https : //ideone.com/ZZ4v6S 似乎C比本地更喜欢全局variables。 这样对吗?

如何在c中指定64位整数

我试图在C中使用64位整数,但得到混合信号,应该是可能的。 当我执行printf时: printf("Size of long int:%d\nSize of long long int:%d\n\n",(int)sizeof(long int), (int)sizeof(long long int)); 我得到的回应是: long int的大小:4 long long int的大小:8 这让我觉得long long int有8个字节= 64位。 但是,当我尝试声明以下variables时: long long int a2 = 0x00004444; long long int b2 = 0x000044440; long long int c2 = 0x0000444400; long long int d2 = 0x00004444000; long long int e2 = 0x000044440000; […]

Sizeof vs Strlen

#include "stdio.h" #include "string.h" main() { char string[] = "october"; // october is 7 letters strcpy(string, "september"); // september is 9 letters printf("the size of %s is %d and the length is %d\n\n", string, sizeof(string), strlen(string)); return 0; } 输出: 9月份的大小是8,长度是9 我的语法有什么问题吗?

sizeof(T)== sizeof(const T)和alignof(T)== alignof(const T)

假设T和const T是两个相同大小和相同排列的types似乎是合理的,但是在考虑了一些真实的系统之后,似乎可能会有所不同。 让我解释: 假设你有一个有两种types的内存的系统:RAM和Flash(它是只读的)。 RAM是8位寻址,而闪存只能寻址16位。 假设这是T : struct T { uint8_t x; uint16_t y; }; 在字节可寻址的RAM中,这个结构的长度是3个字节,但是在双字节可寻址的Flash中(这是一个constvariables所在的地方),因为alignment问题,这个结构必须至less有4个字节长。 所以这是我的问题: c和c ++标准是否保证const和非consttypes的大小和alignment?

string文字的大小

下面的代码 #include <iostream> using namespace std; int main() { const char* const foo = "f"; const char bar[] = "b"; cout << "sizeof(string literal) = " << sizeof( "f" ) << endl; cout << "sizeof(const char* const) = " << sizeof( foo ) << endl; cout << "sizeof(const char[]) = " << sizeof( bar ) […]

sizeof(int)在x64上?

当我在我的C#.NET项目中做sizeof(int) ,我得到的返回值是4.我把项目types设置为x64,那为什么说4而不是8呢? 这是因为我正在运行托pipe代码?

为什么是sizeof(BaseClass)== sizeof(DerivedClass),尽pipe我添加了一个成员

从sizeof(Base) == 24和sizeof(Derived) == 24下面的代码。 为什么他们的大小相等? 在Base类中,我们有3个成员,在Derived类中,我们有另一个成员。 class Base { private: double d; protected: long l; public: int i; }; class Derived : public Base { private: float f; };

在64位机器上应该是sizeof(int)?

可能重复: int,long等的大小 int的大小是否取决于编译器和/或处理器? 什么决定一个整数的大小? 我正在使用一个64-bit机器。 $ uname -m x86_64 $ file /usr/bin/file /usr/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, stripped $ 当我运行下面的程序时,我得到了sizeof(int)为4-bytes 。 #include <stdio.h> int main(void) { printf("sizeof(int) = %d bytes\n", (int) sizeof(int)); return 0; } 如果我运行的是16- 32- , 32-和64-位机器,那么这是不是说integer的大小分别是16-和64-位? 在我的机器上,我发现WORD_BIT是32 。 在64-bit机器上不应该是64 64-bit […]

C中sizeof的内部机制?

我使用sizeof来获取C中的结构体的大小,但是我得到的结果是意外的。 struct sdshdr { int len; int free; char buf[]; }; int main(){ printf("struct len:%d\n",(sizeof(struct sdshdr))); return 0; } //struct len:8, with or without buf 我的问题是为什么buf不占用任何空间,为什么在64位CPU上inttypes的大小仍然是4? 这里是gcc -v的输出: Configured with: –prefix=/Applications/Xcode.app/Contents/Developer/usr –with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.4.0 Thread model: posix