The difference between statically allocated and dynamically allocated memory

To put it bluntly, the difference between static allocation and dynamic allocation of memory is mainly two:
  First, the time is different. Static allocation occurs when the program is compiled and linked. Dynamic allocation occurs when the program is called and executed.
  Second, the space is different. Heaps are all dynamically allocated, there is no statically allocated heap. The stack has two allocation methods: static allocation and dynamic allocation. Static allocation is done by the compiler, such as the allocation of local variables. Dynamic allocation is performed by the function malloc. However, the dynamic allocation of the stack is different from that of the heap. His dynamic allocation is released by the compiler without our manual implementation.

For a process's memory space, it can be logically divided into three parts: code area, static data area and dynamic data area. The dynamic data area is generally the "stack". "Stack" and "heap" are two different dynamic data areas. The stack is a linear structure and the heap is a chain structure. Each thread of the process has a private "stack", so although each thread has the same code, the data of local variables does not interfere with each other. A stack can be described by a "base address" and a "top of stack" address. Global variables and static variables are allocated in the static data area, and local variables are allocated in the dynamic data area, that is, the stack. Programs access local variables through the base address and offset of the stack.
        
Generally, for variables modified with static, global variables are located in the static data area. The parameters, return address, EBP and local variables in the function call process are all stored in the stack.


First of all, before using dynamic memory allocation technology, you must understand what you are doing, how it is different from other methods, and especially what negative effects will be produced. There is no free lunch in the world. The difference between dynamically allocated memory and statically allocated memory:

1) Static memory allocation is done at compile time and does not require CPU resources; dynamic memory allocation is done at runtime, and dynamic memory allocation and release require CPU resources; 
2 ) Static memory allocation is allocated on the stack, and dynamic memory is allocated on the heap; 
3) Dynamic memory allocation requires the support of pointer or reference data types, while static memory allocation does not; 
4) Static allocation of memory needs to determine the size of the memory block before compiling, while dynamic allocation of memory does not need to determine the memory size before compiling. Determine the required memory block size according to the runtime environment, and allocate memory as needed. It can be said that static memory allocation is allocated according to plan, while dynamic memory allocation is allocated on demand. 
5) Statically allocated memory is to give control of memory to the compiler, while dynamic memory is to give control of memory to the programmer; to

sum up, statically allocated memory is suitable for compilation, and it can be determined that it needs to be occupied. Dynamic allocation of memory can be used when the amount of memory required cannot be determined at compile time; however, the operating efficiency of static allocation of memory is higher than that of dynamic allocation of memory, because dynamic memory allocation and release require additional overhead; dynamic The level of memory management depends heavily on the level of the programmer, and it is easy to cause memory leaks if not handled properly. So to be more specific, how to choose the memory allocation method, and what problems should be paid attention to if the memory is allocated dynamically?

It should be emphasized that since the dynamic allocation of memory gives the control of the memory to the programmer, the programmer is obliged to write code to confirm that the memory is allocated as a function. If the allocation fails, it should be handled appropriately, otherwise it will give your program to the next timing. The program may crash at any time due to the failure of dynamic memory allocation.

1. Global variables should not dynamically allocate memory as much as possible.

  Since the variable is defined as a global variable, its visible range is relatively wide, because these variables may be visible during the entire program runtime, and there may be no chance to release the memory occupied by the global variable at all, so using dynamic allocation of memory is Meaningless, it can only bring additional operating burden to the program. 
  However, there may be exceptions when the memory size of global variables cannot be determined. For example, to process a batch of data, the size of the data may be told to the program by the user in the form of console parameters. In this case, the memory can be dynamically allocated on demand and the memory can be used reasonably. 
  For global variables that can determine the amount of memory used at compile time, and the variable working period (let's call it that, that is, the period when the variable may still be used) is the same as the running period of the program, there is no need for dynamic Allocate memory. This situation is very interesting, that is to use dynamic allocation of memory, but you can not consider releasing this memory, because the program will also exit when the memory can be released. Once the program ends, the process will also end, and the virtual space where the entire program is located. All have been freed, so there is no need to add code to free the memory. (But I'm sure I've seen such code)

2. When dynamically allocating memory, the allocation and release code should be symmetrical.

  The code symmetry of allocation and release mentioned here means that the code for allocating memory and the code for releasing memory should be in the same scope of code. For example, when applying for memory at the beginning of a function, the memory should be released at the end of the function, otherwise , if memory is allocated inside a function and released outside the function, it may cause memory leaks due to the negligence of the programmer; if the memory is allocated in the constructor of a class, then the memory should be released in the destructor, Never release it in another function, and wait for the client code to remove the function to manually release the memory. In that case, it is equivalent to burying a timed explosion, which may cause memory leaks due to temporary negligence at any time.

3. Be sure to check the validity of dynamically created objects or allocated memory blocks.

  Due to the concurrency and complexity of the operating system, any dynamic memory allocation operation may fail, especially when a larger block of memory is requested. Therefore, it is necessary to check whether the dynamically created object or the heap memory applied for is successful. Otherwise, program exceptions may be caused by wrong pointers or null pointers. If the exceptions are not properly handled, the entire program may terminate unexpectedly, causing losses.

4. Use dynamic memory allocation as few times as possible.

  Dynamic allocation is done by the operating system at runtime, so it consumes CPU resources, and the allocated resources are as convenient as possible when performing dynamic memory allocation. If the resources you applied for last time are enough, do not re-apply for the resources, and only release the old resources and apply for new resources when they are not enough.

5. On the premise of ensuring resource utilization, static memory allocation can be used instead of dynamic allocation, especially for local temporary objects.

  For example, for local objects, using statically allocated memory, which can be allocated by the compiler at compile time, beyond the scope of automatic memory, not only reduces the program code, reduces the probability of errors, reduces the burden on the programmer, and improves the Program execution efficiency, why not do it?


Reprinted from Jiangsu Sanai Network

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326492508&siteId=291194637