C ++ program memory allocation problem

xiaoxiao2021-03-05  21

Overview:

Objects in C can static allocation - that is, the compiler is allocated when the program source code can also dynamically allocate - that is, the program is executed when the runtime library function is assigned. Static memory allocation is the resulting efficiency relatively high before program execution, while dynamic memory allocation can flexibly process unknown number of elements.

analysis:

Two main differences between static and dynamic memory allocation are: 1. Static object is a name-free variable we do directly and dynamic objects are variables without names, and we operate indirectly through the pointer. 2. The allocation and release of static objects are automatically handled by the compiler. The programmer needs to understand this but does not need to do anything. The allocation and release of the dynamic object must be explicitly managed by programmers, it Different from New and Delete expressions.

Example:

Static allocation: int A = 100; Indicates that the compiler allocates sufficient storage area to store one integer value, which is associated with the name A and then initializes the storage area with a value 10.

Dynamic allocation: 1. Single object: int * a = new int (10); assigned an intra-type object object with an int type of the name is 10, then the expression returns the address of the object in memory, then this address is used To initialize the pointer to the elephant A, the only access method for dynamically assigned memory is indirectly accessed through the pointer. Release method: delete a;

2. Array assignment: int * a = new int [5]; assigned an array containing 5 integer elements, there is no way to explicitly specify an initial value for each element of the array of dynamically assigned an array. Release method: delete [] a;

转载请注明原文地址:https://www.9cbs.com/read-37580.html

New Post(0)