Memory partitions in C + + introduction _c language

Source: Internet
Author: User

C + + memory is divided into stacks, heap areas, global/static areas, string constants, and code areas.

This removes the free storage area and adds a code area, which is the reason for the following.

Stack Area: memory management by the system.

Description: The main storage function parameters and local variables. The stack area is managed by the system, the function completes, the system releases the memory of the stack area, and does not need user management. The size of the stack area of the entire program can be set by the user in the compiler, and the default stack size is 3M.

Global/Static area: Global, static data stored together, initialized global variables and static variables are together. Uninitialized global variables and static variables are in adjacent spaces.

Note: Global variables and static global variables are stored in a consistent manner, but the difference is that global variables can be used throughout the source code, while static global variables can only be valid in the current file. For example, one of our programs has 5 files, then a file application of static global variables, this static global variable can only be used in the current file, the other four files are not available. When a global variable is applied to a file, the global variable can be used in the other four files (just use the keyword extern declaration). In fact, static changes the scope of the variable.

string constant area: holds string constants, which are released by the system after the program is finished. For example, we define char * p = "Hello World"; The "Hello World" here is in string constants, and the final system is automatically released.

Code Area: The binary code that holds the body of the program. For example, the functions we write are in the code area.

Heap Area: Manually manually released by the user. Using malloc in C, use New in C + + (malloc can also be used in C + +).

Description: The new operator essentially uses the malloc for memory applications, so I'll say the free storage and heap areas are stacked, but there is a big difference between the two.

1 malloc is a function in C, and new is an operator in C + +.

2 The type returned after the malloc application is void*, and the pointer returned by new has a type.

3 malloc is responsible for allocating memory only and does not call the constructor of the class, and new will not only allocate memory, but will automatically call the constructor of the class.

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.