VC outputs "hot" and "tun"

Source: Internet
Author: User

I believe that friends who often use VC will not be unfamiliar with a lot of "hot, but it may be strange, why is the word "hot? If the program is modified, the system runs slowly and the fever is too high ??? None! Let me explain it below. Please correct the error:
In the previous article, we reversed a C program in debug mode and found that the storage space opened in the stack zone is filled with 0cccccccch in 4 bytes, that is, each byte of the space opened in the stack area to access local variables is filled with 0xcc. (Why 0xcc is used, which is the machine code of the INT 3 h and used for the next breakpoint) When both 0xcc are output, it exactly corresponds to the Chinese "hot" word.
This is not surprising that the program outputs so much "hot.
------- The end of the paper is shallow, and you must know that this is the case -------
Instance 1:

// If you do not create a project, modify the header file # include "stdafx. H "# include <string. h> int _ tmain (INT argc, _ tchar * argv []) {char s [256] = {0}; // defines an array of memset (S, 0xcc, sizeof (s); // use 0xcc to fill printf ("% s \ n", S); // output return 1 ;}

You can view the running result by yourself:

Program output result

Example 2:

// If you do not create a project, modify the header file # include "stdafx. H "# include <string. h> int _ tmain (INT argc, _ tchar * argv []) {char s [256]; // The system fills printf ("% s \ n ", s); // output return 1 ;}

Program output is still "very hot".

Example 3: define a character array as a global variable

// If you do not create a project, modify the header file # include "stdafx. H "# include <string. h> char s [256]; // global variable int _ tmain (INT argc, _ tchar * argv []) {printf ("% s \ n", S ); // output return 1 ;}

This time the system output is empty and it is not hot! Why, please think for yourself.

In fact, global variables are allocated in the "Global/static storage area", and local variables are allocated in the stack. we can define a large array. If it is local, the program stack overflows because the stack space is limited, but not global.
In addition, you must note that the "hot, hot, and hot" phenomenon only appears in debug mode and does not appear in release mode.
If you are familiar with "tang", have you ever seen it? Yes. I used to encounter "hot", but seldom encountered the latter.
Example 4: dynamically allocate memory space

// If you do not create a project, modify the header file # include "stdafx. H "# include <stdio. h> # include <stdlib. h> int main (INT argc, char * argv []) {char * s = (char *) malloc (sizeof (char) * 256); puts (s ); free (s); Return 0 ;}

When a program is running, the "tun" character on the screen suddenly appears in front of you, and the figure shows the truth:

Program output result

Because the dynamic allocation of space development and heap, VC's debug uses 0xcd to fill the heap space, and the two 0xcd are hidden together.
I tried to trace it through disassembly, but it was really troublesome and I gave up.
The Chinese encoding and variable storage location involved in this article are not described in detail. You can refer to the relevant information on your own.

Original article, reprinted Please note:
This article is from program life>
Are you very hot? I am very hot
Author:Code madman

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.