Learning windows programming (5) modification portal not mainCRTStartup

Source: Internet
Author: User


As mentioned last time, if the entry in the VC console program is not mainCRTStartup, what will happen?

Set the entry to the main function first.


1 # include <stdio. h> 2 3 int main () 4 {5 printf ("hello world"); 6 return 0; 7}

First, you can use the GUI to create a win32 console Application.

 

The code is similar. Print the string.

Compile the link and run it. Of course there is no problem.

Modify the Entry. In the displayed dialog box "project"-> "setting", in the Link option, Enter main in "Entry-point symbol. That is, the entry is defined as the main function.

 

Rebuild, compile the link, no problem.

Deleting intermediate files and output files for project Hello-Win32 Debug.
------------------ Configuration: Hello-Win32 Debug --------------------
Compiling...
Main. cpp
Linking...

Hello.exe-0 error (s), 0 warning (s)

Run.

 

Let's take a look at the problem.

Run the debug version and switch to the error section. VC interface:

 

I don't know if you can see it clearly. I listed the call stack here separately, that is, the red line of the bid.

NTDLL! 7c9100e8 ()
_ Heap_alloc_base (unsigned int 0x00001030) line 161
_ Heap_alloc_dbg (unsigned int 0x00001000, int 0x00000002, const char * 0x00420c1c 'string, int 0x0000003b) line 367 + 9 bytes
_ Nh_malloc_dbg (unsigned int 0x00001000, int 0x00000000, int 0x00000002, const char * 0x00420c1c 'string, int 0x0000003b) line 242 + 21 bytes
_ Malloc_dbg (unsigned int 0x00001000, int 0x00000002, const char * 0x00420c1c 'string, int 0x0000003b) line 163 + 27 bytes
_ Getbuf (_ iobuf * 0x00422a58) line 59 + 19 bytes
_ Flsbuf (int 0x00000068, _ iobuf * 0x00422a58) line 153 + 9 bytes
Write_char (int 0x00000068, _ iobuf * 0x00422a58, int * 0x0012fd10) line 1083 + 75 bytes
_ Output (_ iobuf * 0x00422a58, const char * 0x0042001d, char * 0x0012ff74) line 393 + 21 bytes
Printf (const char * 0x0042001c 'string) line 60 + 18 bytes
Main () line 5 + 10 bytes
KERNEL32! 7c817077 ()

We can see that the error part is in an assembly code in NTDLL, but the root cause is printf. When printf calls this part, the result is wrong when _ heap_alloc_base is called, _ heap_alloc_base literally, we can see that memory is allocated on the heap.

From the previous chapter, we can obtain the mainCRTStartup function called before the main function, which has done a lot of initialization work. One of the function calls is called _ heap_init, is used to create and initialize the CRT heap. If you use main directly for the entry, the init work is not performed.

It was just created through the GUI, and through the command line, the previous cl compilation is the same, and the link is used


1 d: est> link/entry: main hello. obj
2 Microsoft (R) Incremental Linker Version 6.00.8168
3 Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

In addition, if you do not use main and use another function, such as myentry, what will happen? You can try it first. The next article will explain it in detail ~

 

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.