Differences between local variables, local static variables, global variables and Global static variables

Source: Internet
Author: User

How many areas are c ++ memory allocated?
I:

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.

II:

1. Stack is the storage area for variables allocated by the compiler when needed and automatically identified when not needed. The variables are usually local variables and function parameters.

2. Heap refers to the memory blocks allocated by new. Their release compiler is not controlled and controlled by our application. Generally, a new compiler corresponds to a delete. If the programmer does not release the program, the operating system will automatically recycle it after the program is completed.

3. The free storage zone is the memory blocks allocated by malloc and so on. It is very similar to the heap, but it uses free to end its own life.

4. In the global/static storage area, global variables and static variables are allocated to the same memory. In the previous C language, global variables were divided into initialized and uninitialized ones, in c ++, there is no such distinction. They share the same memory zone.

5. Constant storage, which is a special storage area, stores constants and cannot be modified.

III:

1. bss is short for block started by symbol. It is usually used to store a memory area of uninitialized global variables in the program. When the program is loaded, it is cleared by the kernel. Bss segments belong to static memory allocation. The initial value is determined by the user-defined connection location file. You should define it in the read/write ram zone. The memory allocated by malloc in the source program is the same, it is not determined based on the data size, mainly determined by the maximum size of memory allocated in the program at the same time. However, if the size exceeds the range, that is, the allocation fails, it can be distributed after the space is released.

2. The text segment is a program code segment. In the at91 library, it indicates the size of the program segment, which is automatically calculated by the compiler during compilation and connection, when you place the symbol in the code segment in the Link location file, the value indicated by the symbol is the code segment size. when compiling the connection, the value represented by this symbol is automatically substituted into the source program.

3. data contains static initialization data, so global variables and static variables with initial values are in the data zone. The starting position of the segment is determined by the connection location file. The size is automatically allocated when the connection is compiled. It has nothing to do with the size of your program, but it is a global variable used by the program, constant quantity.

4. stack stores the local variables and parameters of the function. It is a data structure of "last in first out (lifo)", which means that the data that is last put on the stack will be the first data that is removed from the stack. Lifo is an ideal data structure for temporary storage information and information that does not need to be stored for a long time. After a function or process is called, The system usually clears the local variables, function call information, and other information stored on the stack. Another important feature of stack is that its address space is "reduced down", that is, the more data stored on the stack, the lower the stack address. The top of the stack is at the end of the read/write ram zone.

5. The heap Storage function dynamically allocates memory. It is another data structure used to save program information. More accurately, it is the dynamic variable used to save the program. Heap is the first in first out (fifo) data structure. It only allows data insertion at one end of the heap and Data removal at the other end. The heap address space is "Increased up", that is, the more data is stored on the heap, the higher the heap address.

Conclusion (not sure !!!) :

The research is of little significance. Different compilers may behave differently. If it is vc, it is basically as follows:

The Code area is an exe segment generated by the compiler and has readable and executable attributes. However, if dep Data Execution Protection is not enabled, all sections are executable.

The so-called stack zone has a low address (less than the exe base address) and can read/write attributes. The exe does not have a corresponding segment, which is automatically generated when the system loads the dll, since the memory address usage is reduced from large to small, the quantity is limited. do not define too large array variables. The local variables of const are also placed in the stack, rather than in the constant zone.

The so-called heap zone refers to memory segments such as malloc and new. It has read/write attributes and does not have corresponding segments in the exe. It is automatically generated when the system loads the dll, the first step is to use the section below the stack address, which is also a low address. When it is used up, a slightly higher address will be automatically allocated (greater than the exe base address ). Both malloc and new are allocated memory here.

The global data zone is an exe segment generated by the compiler. It has read/write attributes. Both the initial and uninitialized global and static variables are stored here.

The constant area is an exe segment generated by the compiler. It has only readable attributes, such as char s = "hello world". At this time, "hello world" is in the constant area. Because there is no writable attribute, therefore, an error occurs in the modification content, and the global const variable is also placed in the constant area, which is inconsistent with the storage location of the const variable in the c ++ programming language, because there are various differences in memory.

 


Differences between local variables, local static variables, global variables, and Global static variables:

Local variable: Stack Zone
Local static variables: static Zone
Global variable: constant zone of the static Zone
Global static variable: static Zone

In C/C ++ programming, programmers must have a more accurate understanding of the memory. Memory that often needs to be operated can be divided into the following categories:
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.
The following is a piece of actual program code:

This is written by a senior. It is very detailed.
// Main. cpp
Int a = 0; global initialization Zone
Char * p1; uninitialized globally
Main ()
{
Int B; stack
Char s [] = "abc"; stack
Char * p2; stack
Char * p3 = "123456"; 123456 is in the constant zone, and 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 is placed in the constant area, and the compiler may optimize it to the "123456" that p3 points.
}


Ii. Theoretical knowledge of heap and stack
2.1 Application Method
Stack:
Automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.
Heap:
The programmer needs to apply and specify the size. In c, the malloc Function
For example, p1 = (char *) malloc (10 );
Use the new operator in C ++
For example, p2 = (char *) malloc (10 );
But note that p1 and p2 are in the stack.
2.2
System Response after application
STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application,
The linked list is traversed to find the heap node with the first space greater than the requested space. Then, the node is deleted from the idle node linked list and allocated to the program, for most systems, the size of the allocation will be recorded at the first address in the memory space, so that the delete statement in the code can correctly release the memory space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list.
2.3 Application size limit
STACK: in Windows, a stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. In WINDOWS, the stack size is 2 MB (OR 1 MB, in short, it is a constant determined during compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, the space available from the stack is small.
Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.
2.4 comparison of application efficiency: www.2cto.com
The stack is automatically allocated by the system, which is faster. But programmers cannot control it.
The heap is the memory allocated by new, which is generally slow and prone to memory fragments. However, it is most convenient to use.
In addition, in WINDOWS, the best way is to use VirtualAlloc to allocate memory. Instead of heap or stack, it is to reserve a fast memory in the address space of the process, although it is the most inconvenient to use. But the speed is also the most flexible
Storage content in 2.5 heap and stack
STACK: when calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), and then the parameters of the function, in most C compilers, parameters are written from right to left into the stack, followed by local variables in the function. Note that static variables are not included in the stack.
When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function, where the program continues to run.
Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.
2.6 comparison of access efficiency

Char s1 [] = "aaaaaaaaaaaaa ";
Char * s2 = "bbbbbbbbbbbbbbbbb ";
Aaaaaaaaaaa is assigned a value at the runtime;
Bbbbbbbbbbbbb is determined during compilation;
However, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap.
For example:
# Include
Void main ()
{
Char a = 1;
Char c [] = "1234567890 ";
Char * p = "1234567890 ";
A = c [1];
A = p [1];
Return;
}
Corresponding assembly code
10: a = c [1];
00401067 8A 4D F1 mov cl, byte ptr [ebp-0Fh]
0040106A 88 4D FC mov byte ptr [ebp-4], cl
11: a = p [1];
0040106D 8B 55 EC mov edx, dword ptr [ebp-14h]
00401070 8A 42 01 mov al, byte ptr [edx + 1]
00401073 88 45 FC mov byte ptr [ebp-4], al
The first method reads the elements in the string directly into the cl register. The second method reads the characters in the specified edx according to edx, which is obviously slow.

From http://hi.baidu.com/evilrapper/blog/item/1434298f0e28f8e4503d9278.html

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.