sizeof

sizeof('z') result unexpected [duplicate]

大憨熊 提交于 2019-12-12 10:27:05
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Size of character ('a') in C/C++ Why does this program output 4 and not 1? void main() { printf("%d",int(sizeof('z'))); } 'z' is a character and sizeof('z') must print 1? 回答1: 'z' is a character literal and in C a character literal is of type int . So sizeof('z') equals sizeof(int) on your implementation. 回答2: Sizeof char. Perhaps surprisingly, character constants in C are of type int , so sizeof('a') is sizeof

Can size of pointers to non-union classes differ?

廉价感情. 提交于 2019-12-12 08:41:10
问题 I understand there are HW platforms where you need more information to point to a char than you need to point to an int (the platform having non-addressable bytes, so a pointer to char needs to store a pointer to a word and also an index of a byte in the word). So it is possible that sizeof(int*) < sizeof(char*) on such platforms. Can something similar happen with pointers to non-union classes? C++ allows covariant returns types on virtual functions. Let's say we have classes like this:

How do you know how much space to allocate with malloc()?

删除回忆录丶 提交于 2019-12-12 07:50:53
问题 I'm a total C newbie, I come from C#. I've been learning about memory management and the malloc() function. I've also came across this code: char *a_persons_name = malloc(sizeof(char) + 2); What I don't understand is how much space this is allocating for a_persons_name . Is it allocating 2 characters (eg. AB) or something else? I also know that you can sometimes get "lucky" with malloc and use unallocated space (which can result in data corruption and seg faults). So how do I know how much

Determine `sizeof float` without compilation

穿精又带淫゛_ 提交于 2019-12-12 07:44:01
问题 I'd like to know the size of a float in GCC, without having to run the compiler. I know one option is to write a small function and have the compiler print out an assembly listing. There is limits.h , which contains the minimums and maximums, but is there something similar that tells the size of the different implicit types? I'm using GCC on Windows 7 x64; the target platform is ARM7 32 bit mode. Language is C. 回答1: You can have GCC print out all of the default macros: gcc -dM -E - </dev/null

C 可变参数函数的本质

邮差的信 提交于 2019-12-11 17:39:49
C语言支持定义可变参数的函数,方法是在函数的参数列表最后加上 " ... ",代表变长的参数列表,例如: void Func( int num, ...) { } 需要注意 “...” 必须在最后,而且前面 起码要有一个固定的参数 ,类型可以任意。 为什么要有一个固定的参数呢?这篇文章要说明的就是这个问题。 首先我们是如何调用变长参数列表里的变量? 需要使用 stdarg.h 里定义的三个宏: va_start(ap, x)、va_arg(ap,t)、va_end(ap) ,还有一个v a_list 类型(本质上是字节指针) 这几个宏的源代码: 1 typedef char * va_list; 2 3 #define _INTSIZEOF(n) ((sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 4 5 #define __crt_va_start_a(ap, v) ((void)(ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v))) 6 #define __crt_va_arg(ap, t) (*(t*)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t))) 7 #define __crt_va_end(ap) ((void)(ap = (va_list)0))

Explain the result of sizeof operator for a union containing structures

不打扰是莪最后的温柔 提交于 2019-12-11 16:26:27
问题 #include<stdio.h> struct mystruct { char cc; float abc; }; union sample { int a; float b; char c; double d; struct mystruct s1; }; int main() { union sample u1; int k; u1.s1.abc=5.5; u1.s1.cc='a'; printf("\n%c %f\n",u1.s1.cc,u1.s1.abc); k=sizeof(union sample); printf("%d\n\n",k); return 0; } The size of operator is returning 8 I am still able to access the structure elements, more than one at a time and still the sizeof operator is returning the max size of primitive data types i assume. Why

Reading 16-bit integers from binary file c++

让人想犯罪 __ 提交于 2019-12-11 13:45:34
问题 I'm not sure if I'm doing this right so I want to check my code. it works but I'm not sure its working right. I need it to read the binary file, and store the 16 bit integers in an array of ints that is the exact size needed. I tried to do sizeof(storage[i]) so I could see if I was storing 16 bits but it says 32 (I'm guessing because int automatically allocates 4 bytes? void q1run(question q){ int end; std::string input = q.programInput; std::ifstream inputFile (input.c_str(), ios::in | ios:

Can't output int as result of sizeof()

不问归期 提交于 2019-12-11 13:36:15
问题 I'm lloking to write a program in C++ which will sync two directories for me, and for part of it I need to get the size of an char array. The code I'm using is below #include <iostream> #include <Windows.h> using namespace std; int main() { char binaryPath[MAX_PATH]; GetModuleFileName(NULL, binaryPath, MAX_PATH); int r = sizeof(binaryPath); return 0; } Anyway, that code compiles and runs fine. The problem comes when I try and cout the binaryPath variable. In the main block, after the

C++ Class/Structure size

☆樱花仙子☆ 提交于 2019-12-11 13:35:07
问题 Hi I have some problem about the size of a class/struct Here is my Graphnode.h, I only have 4 vars in it- one 16-unsigned char array, three unsigned char, I think the size should be 19. Why is it 32? Graphnode currentNode; cout<< sizeof(currentNode)<<endl;// why this is 32 ? cout<< sizeof(currentNode.state)<< endl;// this is 16 Graphnode.h: #include <stdio.h> #include <stdlib.h> #include <tr1/array> //using namespace std; class Graphnode { public: std::tr1::array<unsigned char, 16> state;

Why struct size is multiple of 8 when consist a int64 variable In 32 bit system

感情迁移 提交于 2019-12-11 13:22:20
问题 In C Programming language and I use 32 bit system, I have a struct and this struct size is multiple of four. But I look at Linker Map file and size is multiple of eight Example typedef struct _str { U64 var1, U32 var2 } STR; Size of this struct is 16. But typedef struct _str { U32 var1, U32 var2, U32 var3 } STR2; Size of STR2 is 12. I am working on 32 bit ARM microcontroller. I dont know why 回答1: The size of a structure is defined as a multiple of the alignment of a member with the largest