Differences between static and dynamic memory allocation

Source: Internet
Author: User

To understand this problem, you must first know what static and dynamic means. I personally think the explanation on Carnegie card is classic:

"The wordStaticRefers to things that happen at compile time and link time when the program is constructed-as opposed to load time or run time when the program is actually started ."

"The termDynamicRefers to things that take place when a program is loaded and executed ."

To put it bluntly, there are two main differences between static and dynamic memory allocation:

First, the time is different. Static allocation occurs during program compilation and connection. Dynamic Allocation occurs when the program is called in and executed.

Second, the space is different. The heap is dynamically allocated without static allocation. There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is completed by the compiler, such as local variable allocation. The function malloc allocates dynamic allocation. However, the stack's dynamic allocation is different from the heap's. Its Dynamic Allocation is released by the compiler without manual implementation.

The memory space of a process can be logically divided into three parts: Code zone, static data zone, and dynamic data zone. The Dynamic Data zone is generally a "stack ". "Stack" and "Heap" are two different dynamic data zones. Stack is a linear structure, and stack is a chain structure. Each thread of a process has a private "stack". Therefore, although the Code of each thread is the same, the data of local variables does not interfere with each other. A stack can be described through the "base address" and "Stack top" addresses. Global and static variables are distributed in the static data area, and local variables are distributed in the dynamic data area, that is, the stack. The program accesses local variables through the base address and offset of the stack.

Generally, variables modified with static are located in the static data zone. Parameters, return addresses, EBP, and local variables in the function call process are stored in stacks.

Others:

The so-called dynamic memory allocation refers to the way in which the allocated memory of the storage space is dynamically allocated or recycled during program execution. Unlike static memory allocation methods such as arrays, dynamic memory allocation requires pre-allocation of storage space. Instead, the system allocates the storage space in real time based on program requirements, and the allocated size is the size required by the program.

For example, we define a float array: Float score [100];

However, when using arrays, there is always a problem: How big should an array be? In many cases, you cannot determine the size of the array to be used. For example, in the previous example, you may not know the size of the array to be defined, then you need to define the array to be large enough. In this way, your program will apply for a fixed size of memory that you think is large enough. Even if you know the size of the space you want to use, if the size of the space you want to use increases or decreases for some special reason, you must modify the program again to expand the storage range of the array. This method of allocating a fixed size of memory is called static memory allocation. However, this memory allocation method has serious defects, especially when dealing with some problems: In most cases, a large amount of memory space is wasted. In a few cases, when the defined array is not large enough, it may cause subscript out-of-bounds errors or even serious consequences.

We can use dynamic memory allocation to solve the above problem. The so-called dynamic memory allocation refers to the way to dynamically allocate or recycle the memory allocated during program execution. Unlike static memory allocation methods such as arrays, dynamic memory allocation requires pre-allocation of storage space. Instead, the system allocates the storage space in real time based on program requirements, and the allocated size is the size required by the program. From the above comparison, we can know the characteristics of dynamic memory allocation compared with Jingtai memory allocation:

1. No pre-allocated storage space is required;

2. The allocated space can be expanded or reduced as needed by the program.

To dynamically allocate storage space based on program needs, you must use the malloc function.

The prototype of the malloc function is void * malloc (unsigned int size). It is used to allocate a continuous space with a length of size in the dynamic storage area of the memory. The parameter is an unsigned integer, And the return value is a pointer to the starting address of the allocated continuous storage domain. Note that a null pointer is returned when the function fails to allocate storage space (such as insufficient memory. Therefore, when calling this function, check whether the returned value is null and perform corresponding operations.

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.