Differences between dynamically allocated and statically allocated data in memory

Source: Internet
Author: User

Use molloc in C and free to dynamically allocate and release memory, while the new Delete operator can be used in C ++. For example, we can define arrays in C in two ways:
1. Static Memory Allocation: for example, int A [10];
2. Use the new dynamic definition: for example, int * P = new int [10];
So what are the differences between the two methods ??
In fact, the use of new, delete, and so on is called "dynamic memory allocation". We generally define the entire life cycle of the int A [10] array existence function, that is, if an array a [10] is defined in a function, the array will exist from the time it is defined until the execution of the function is completed. It is static and cannot be Program Members are automatically assigned and released. The so-called "dynamic memory allocation" is dynamic because we can define the allocation and release at any time in the function, which is a big difference between dynamic allocation and general space allocation. In addition, we generally define a [10]. If it is defined inside the function, the stack space of the stored memory is not modified in static mode, the data in this space is automatically allocated and released by the compiler. The dynamically allocated data space is stored in the heap. Generally, it is allocated and released by the programmer. The system will not be automatically released, therefore, if the new function is used to dynamically allocate heap space in the memory, delete must be used to release the function, even if the function has been executed.
The memory occupied by C/C ++ programs is divided into the following parts:
1. Stack );
The compiler automatically allocates and releases, stores function parameter values, local variable values, and so on. The stack comes out first. We can see that the stack is regarded as a memory zone for registers and temporary data exchange.
2. Heap)
Generally, programmers use functions such as malloc to allocate and release data. If the programmer does not release the data, the operating system may not recycle the data when the program runs.
3. Global zone (static zone)
The global variables and local static variables are stored here. The initialized global variables and static variables are in one area, the uninitialized global variables and initialized static variables are located in an adjacent area (usually called the BSS area). After the program ends, the system releases them. The heap size is not fixed and can be dynamically expanded or reduced.
4. Text Constant Area
Constant strings are stored here. The program is released by the system after it is accepted;
5. Program Code Zone
Stores the binary code of the function body;
Theoretical knowledge of heap and stack
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 ++
But note that P1 is in the stack.
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, it will traverse the linked list, find the heap node with the first space greater than the requested space, delete the node from the idle node linked list, and allocate the space of the node to the program. In addition, 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.
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.
4 Comparison of application efficiency:
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. However, it is fast and flexible.
5 Storage content in 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.
6 Comparison of access efficiency
Char S1 [] = "aaaaaaaaaaaaa ";
Char * S2 = "bbbbbbbbbbbbbbbbb"; // is this string in the heap ???????????????
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 <stdio. h>;
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 type reads the elements in the string directly into the CL register, while the second type reads the pointer value into EDX. Reading the characters based on edX is obviously slow.
Note: This article is the materials prepared by tekuba in the early stage. No reference link is left. If the link is infringing, please inform teku.

This article from teku bar http://www.tekuba.net/, the original address: http://www.tekuba.net/program/168/

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.