How should the memory be allocated in the program? How to perform dynamic memory allocation?

The program is stored in segments in the memory.

Code segment: store the number converted by the statement; cannot be modified when the program is running; global segment: used to record the storage location of global variables and static variables; will not change with the running of the program; stack: store local variables, block variables, and forms The storage location of parameters and return values; its size will continue to change as the program runs; when a function is called, the space is opened up, and the space is reclaimed when the function is called; the last-in-first-out principle is followed between different calling functions. Heap: a storage location for storing dynamic distribution;

One: Disadvantages of traditional arrays (static arrays)

1: The length of the array must be specified in advance, and is a constant integer, not a variable int a[5]; 2: The traditional array programmer cannot be released by the programmer, but can only be released by the system. (And it can only be released at the end of the function where the array is located) 3: The length of the array cannot be dynamically expanded or reduced during the running of the function 4: The traditional array defined by the A function cannot be used in the B function at the end of the A function , Because it has been released. That is, traditional arrays cannot span functions.

Distinguish: static storage and static development of memory

Two: Why do we need to dynamically allocate memory

Used to solve the four defects of traditional arrays

Three: Examples of dynamic memory allocation, and the construction of dynamic arrays

Method: malloc function to open up space in the heap

1: malloc is dynamically opened up by the programmer on the stack 2: the first address of the return value opened up space, but the type is void *, which requires forced type conversion 3: the allocated memory space should be able to divide the number of bytes occupied by the type 4: Include the header file malloc.h5: only free (p) can be used to release the dynamically opened memory space pointed to by p. 6: For the operation of dynamic memory space, use *p to operate. 7: You can use multiple pointers to point to this dynamic space. 8: When there are multiple pointers that only point to this dynamic space, you can only use a free pointer. Repeated releases will cause an error. 9: You can use the dynamically opened memory pointer as Function parameters

Question: Is the allocation type of p dynamic or static? After calling free(p), will the memory space of p be released? Example: Dynamically construct a one-dimensional array:

Dynamically expand the length of the array, that is, dynamically expand when the program is running: realloc (first address, total number of bytes) Function: re-open up the original dynamic memory dynamically opened up by a number of bytes, if this number is larger than before, the front The data is saved. If it is smaller than the original, keep the previous data.

Note: The expanded or scaled memory unit is a newly developed memory unit. There is a valuable copy process in this process. The return value is the first address of the newly opened address space.

4: Comparison of static memory and dynamic memory

Statically created memory: created in the stack, allocated by the compiler, and automatically released by the system. Memory created dynamically: created in the heap, created by the programmer, and released automatically by the programmer.

Five: the problem of using memory across functions

Rechargeable Vape

Are rechargeable vapes good?Of course, the rechargeable e-cigarette can better burn the smoke oil, prolong the use time of the Disposable Vape, and from then on, it is no longer limited by the battery capacity, and can have a larger number of puffs. This is a relatively new e-cigarette manufacturing technology recently. We are an disposable vape manufacturer from China, and we also sell many popular brand Vape Pen disposable pods.

Rechargeable Vape,Mod Rechargeable Disposable Vape,Hyde Edge Recharge E-Cigarette,Rechargeable disposable E-Cigarette, Rechargeable Battery Vape

TSVAPE Wholesale/OEM/ODM , https://www.tsecigarette.com

This entry was posted in on