C language code and various constants, variable storage location in memory and Memory Optimization

Source: Internet
Author: User

The global variables and static local variables are stored in the global data zone. The initialized and uninitialized variables are saved together;

Common local variables are stored in the stack;

What is the difference between global variables and local variables in memory?

I. prerequisites-program memory allocation

The memory occupied by a c/C ++ compiled program is divided into the following parts:

1. stack: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation method is similar to the stack in the data structure.

2. heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.


3. Global (static)-the storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The system is released after the program ends.

4. Text Constant Area-constant strings are placed here. The program is released by the System

5. program code area-stores the binary code of the function body.

Note:
As I personally understand, in order to reduce the generation of memory fragments, the compiler may divide the heap area into blocks and heap areas. A block consists of a series of memory blocks of the same size. When allocating memory, it is allocated in the block first. If the block is full, it is allocated from the heap area. At the same time, you can configure the block size and number through the configuration file to make it an appropriate number.

Example:


/* =================================== */
/* Heap conf */
/* =================================== */
/* General configuration for both linear heap and block based heap
* Define this list to the size and count of individual fixed-size pools
*/
# Define BLOCK_LIST \
/* Size count */\
BLOCK (22, 80 )\
BLOCK (44, 64 )\
BLOCK (56, 16)
 
Ii. Example Program

// Main. cpp

Int a = 0; global initialization Zone

Char * p1; uninitialized globally

Main ()

{

Int B; // Stack

Char s [] = "abc"; // "abc" is in the constant area, and s is in the stack.

Char * p2; // Stack

Char * p3 = "123456"; // 123456 \ 0 "; in the constant area, p3 is on the stack.

Static int c = 0; // global (static) initialization Zone

P1 = (char *) malloc (10 );

P2 = (char *) malloc (20 );

// The allocated 10-byte and 20-byte areas are in the heap area.

Strcpy (p1, "123456"); // 123456 \ 0 is placed in the constant area, and the compiler may optimize it into a place with the "123456" pointed to by p3.

}

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.