In C, the handling of sizeof () is done in the compile phase.
#include <Windows.h>#include<stdio.h>intMessageboxprint (Char*Szformat, ...);intFunCharch[]);intWinMain (hinstance hInst, hinstance tmp, LPSTR strcmd,intnshow) { Charstr[1024x768]; Messageboxprint ("%p", HInst); Messageboxprint ("ch=%d", Fun (str)); return 0;}intMessageboxprint (Char*Szformat, ...) { Charbuf[1024x768]; Va_list va; Va_start (VA, szformat); vsnprintf (BUF,sizeof(BUF), Szformat, VA); Va_end (VA); returnMessageBox (Null,buf,"printf", MB_OK);}intFunCharch[]) { return sizeof(CH);}
View Code
sizeof calculates in the fun function is the length of the pointer ch, 32-bit OS constant 4
sizeof calculates the string containing where to look, it is difficult to estimate the risk when writing the code, it is best to use strlen
Modify the following
#include <Windows.h>#include<stdio.h>intMessageboxprint (Char*Szformat, ...);intFunCharch[]);intWinMain (hinstance hInst, hinstance tmp, LPSTR strcmd,intnshow) { Charstr[1024x768]; Messageboxprint ("%p", HInst); Messageboxprint ("ch=%d", Fun (str)); return 0;}intMessageboxprint (Char*Szformat, ...) { Charbuf[1024x768]; Va_list va; Va_start (VA, szformat); vsnprintf (BUF,sizeof(BUF), Szformat, VA); Va_end (VA); returnMessageBox (Null,buf,"printf", MB_OK);}intFunCharch[]) { returnstrlen (CH);}
View Code
There is also a problem, because WinMain inside STR is not initialized, strlen calculate the length of time, the length is unknown. Because he's going to find it all the time. The length of the strlen may be greater than 1024, possibly equal to 1024, or less than 1024.
Windows Programming-Dangerous sizeof