sizeof

calling sizeof on a function call skips actually calling the function!}

好久不见. 提交于 2019-12-01 17:00:49
问题 I happened to stumble across this piece of code. int x(int a){ std::cout<<a<<std::endl; return a + 1; } int main() { std::cout<<sizeof(x(20))<<std::endl; return 0; } I expected it to print 20 followed 4. But it just prints 4. Why does it happen so? Isn't it incorrect to optimize out a function, that has a side effect (printing to IO/file etc)? 回答1: sizeof is a compile-time operator, and the operand is never evaluated. 回答2: sizeof is actually an operator and it is evaluated in compile-time.

calling sizeof on a function call skips actually calling the function!}

北城以北 提交于 2019-12-01 16:54:21
I happened to stumble across this piece of code. int x(int a){ std::cout<<a<<std::endl; return a + 1; } int main() { std::cout<<sizeof(x(20))<<std::endl; return 0; } I expected it to print 20 followed 4. But it just prints 4. Why does it happen so? Isn't it incorrect to optimize out a function, that has a side effect (printing to IO/file etc)? sizeof is a compile-time operator, and the operand is never evaluated. sizeof is actually an operator and it is evaluated in compile-time. The compiler can evaluate it because the size of the return type of x is fixed; it cannot change during program

【转】别人写的pe代码

余生颓废 提交于 2019-12-01 16:34:04
// PEOperate.cpp: implementation of the PEOperate class. // ////////////////////////////////////////////////////////////////////// #include "PEOperate.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// #include "windows.h" #include "stdio.h" #include "string.h" #define MESSAGEBOXADDR 0x77D507EA #define SHELLCODELENGTH 0x12 BYTE shellCode[]={ 0x6A,00,0x6A,00,0x6A,00,0x6A,00, 0xE8,00,00,00,00, 0xE9,00,00,00,00 }; //加载PE文件到内存中 LPVOID ReadPEFile(LPSTR lpszFile) { FILE *pFile =

C++ sizeof wrapper class

邮差的信 提交于 2019-12-01 16:01:37
Suppose I have a class A that does not inherit from anything, has no virtual methods, and has exactly one variable of type T. Does C++ guarantee sizeof(A) == sizeof(T) ? EDIT: Also if T were a composite type, would it make a difference? No, it might be more than sizeof(T) due to padding. Seth Carnegie I don't think it explicitly guarantees it, but I don't think it would ever be different. I think C++ should guarantee sizeof(A) == sizeof(T). Consider bellow situation, C++ should make it works just like in C: A a[10]; T t[10]; A * ap = (A *) t; T * tp = (T *) a; memcpy(ap, tp, sizeof(*ap)); 来源:

Why does sizeof(char + char) return 4?

本秂侑毒 提交于 2019-12-01 15:49:54
char a, b; printf("%d", sizeof(a+b)); What will printf write to the screen? I thought because sizeof(char)=1, that sizeof(a+b) will be also 1, but it turned out to be 4. I don't understand this, why does it write 4 if we are adding two chars? In C language operands of almost all arithmetic operators are subjected to implicit conversions called usual arithmetic conversions or, in this case, integer promotions . Operands of type char are promoted to type int and the actual addition is performed within the domain of int (or unsigned int , depending on the properties of char on that platform). So

Printing a char with printf

这一生的挚爱 提交于 2019-12-01 15:44:34
Are both these codes the same char ch = 'a'; printf("%d", ch); Will it print a garbage value? I am confused about this printf("%d", '\0'); Will this print 0 or garbage value? Because when i do this printf("%d", sizeof('\n')); It prints 4. Why is sizeof('\n') 4 bytes? The same thing in C++ prints 1 bytes. Why is that? So here's the main question in c language is printf("%d", '\0') supposed to print 0 and in C++ printf("%d", '\0') supposed to print garbage? %d prints an integer: it will print the ascii representation of your character. What you need is %c : printf("%c", ch); printf("%d", '\0');

Why does sizeof(char + char) return 4?

荒凉一梦 提交于 2019-12-01 14:57:40
问题 char a, b; printf("%d", sizeof(a+b)); What will printf write to the screen? I thought because sizeof(char)=1, that sizeof(a+b) will be also 1, but it turned out to be 4. I don't understand this, why does it write 4 if we are adding two chars? 回答1: In C language operands of almost all arithmetic operators are subjected to implicit conversions called usual arithmetic conversions or, in this case, integer promotions . Operands of type char are promoted to type int and the actual addition is

Why isn't the size of a record equal to the sum of the sizes of its fields?

做~自己de王妃 提交于 2019-12-01 14:57:23
问题 I have next code: type TRecord1 = record myarr: array [0..31] of single: end; type TRecord2 = record b1, b2, b3, b4, b5, b6: byte; end; type TRecord3 = record myarr: array [0..31] of single: b1, b2, b3, b4, b5, b6: byte; end; procedure TForm1.FormCreate(Sender: Tobject); begin ShowMessage(IntToStr(SizeOf(TRecord1))+'+'+IntToStr(SizeOf(TRecord2))+ '='+IntToStr(SizeOf(TRecord3))); end; The program shows the following message: 128+6=136 Why is SizeOf(TRecord3) equal to 136 rather than 134? 回答1:

Printing a char with printf

北慕城南 提交于 2019-12-01 13:47:47
问题 Are both these codes the same char ch = 'a'; printf("%d", ch); Will it print a garbage value? I am confused about this printf("%d", '\0'); Will this print 0 or garbage value? Because when i do this printf("%d", sizeof('\n')); It prints 4. Why is sizeof('\n') 4 bytes? The same thing in C++ prints 1 bytes. Why is that? So here's the main question in c language is printf("%d", '\0') supposed to print 0 and in C++ printf("%d", '\0') supposed to print garbage? 回答1: %d prints an integer: it will

How to get the size of dynamically allocated 2d array

£可爱£侵袭症+ 提交于 2019-12-01 13:32:21
问题 I have dynamically allocated 2D array. Here is the code int **arrofptr ; arrofptr = (int **)malloc(sizeof(int *) * 2); arrofptr[0] = (int *)malloc(sizeof(int)*6144); arrofptr[1] = (int *)malloc(sizeof(int)*4800); Now i have to know that how many bytes are allocated in arrofptr,arrofptr[0],arrofptr[1]? is there any way to know the size? if we will print sizeof(arrofptr); sizeof(arrofptr[0]); sizeof(arrofptr[1]); then it will print 4. 回答1: You can't find size of arrofptr , because it is only a