Windows Programming-Dangerous sizeof

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.