Memory Allocation in C

Source: Internet
Author: User
The memory occupied by a C program can be divided into the following categories: 
(1) Stack 
This is the region automatically allocated and released by the compiler. It mainly stores function parameters and local variables of functions. When a function starts to execute, partial variables are pushed into the stack for the real parameters required by the function. After the function is executed, parameters and variables that have previously entered the stack are also released. Its running mode is similar to the stack in the data structure. 
(2) Heap 
This is a region controlled by programmers for allocation and release. In C, the space allocated by using the malloc () function exists on the stack. The space allocated on the stack is not automatically released after a function is executed like the stack, but exists throughout the running of the program. Of course, if you do not manually release (free () function) these spaces, the system will automatically release them after the program runs. For small programs, the impact may not be felt, but for large programs, such as a large game, there will be a problem of insufficient memory. 
(3) Global Zone 
The global variables and static variables in C are stored in the global zone. They are a bit like the space on the stack, and they also exist continuously. 
But the difference is that they are allocated and released by the compiler itself. 
(4) text Constant Area 
For example, if char * c = "123456", "123456" is a text constant, which is stored in the text constant area. It is also controlled by the compiler for allocation and release. 
(5) code area 
Stores the binary code of the function body. 

2. Example (1) 
Int A = 0; // global Zone

Void main ()
{
Int B; // Stack
Char s [] = "ABC"; // s in the stack, "ABC" in the text Constant Area
Char * P1, * P2; // Stack
Char * P3 = "123456"; // "123456" in the constant area, P3 in the stack
Static int C = 0; // global Zone
P1 = (char *) malloc (10); // P1 is on the stack, and the allocated 10 bytes are on the heap 
P2 = (char *) malloc (20); // P2 on the stack, 20 bytes allocated in the heap 
Strcpy (P1, "123456"); // put "123456" in the constant area 
// The Compiler may optimize it to the "123456" that P3 points.

3. Example (2) 
// Returns the char pointer. 
Char * F () 

// The s array is stored on the stack. 
Char s [4] = {'1', '2', '3', '0 '}; 
Return s; // return the address of the S array, but the S array is released after the program runs. 

Void main ()
{
Char * s; 
S = f (); 
Printf ("% s", S); // print out garbled characters. Because s points to the address, there is no data 
}

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.