1. How to allocate memoryThere are three ways to allocate memory: [1] from a static storage area. Memory is allocated at the time of program compilation, and the entire running period of the program is present in this block. For example, global variables, static variables. [2] created on the stack. When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity. [3] Allocated from the heap, also known as dynamic memory allocation. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by the programmer and is very flexible to use, but if space is allocated on the heap, it is the responsibility to reclaim it, otherwise the running program will have a memory leak, and frequently allocating and releasing different sizes of heap space will result in heap fragments.
2. Memory space of the program
A program allocates the operating system to its running memory block into 4 regions, as shown in the following figure
Code Area |
Program Memory Space |
Global Data area |
Heap Area |
Stack area |
The memory used by a program compiled by C + + is divided into the following sections: 1. Stack (stack) is automatically allocated by the compiler to dispose of local variables, function parameters, return data, return addresses, etc. that are allocated for running functions. It operates in a manner similar to a stack in a data structure. 2, heap area (heap) is usually allocated by the programmer release, if the programmer does not release, the program ends may be recycled by the OS. The allocation method is similar to a linked list. 3. Global zone (static zone), which holds global variables, static data, constants. System release after the end of the program 4, the literal constant area constant string is placed here. Released by the system after the program is finished. 5. The program code area holds binary code for the function body (class member functions and global functions).
For example:
int a = 0; Global initialization zone
char *p1;//global uninitialized Zone
int main () {
int b;//Stack
char s[] = "abc";//stack
char *p2;//Stack
char *p3 = "123456";//123456\\0 in the constant area, p3 on the stack.
static int c =0;//Global (static) initialization zone
p1 = new char[10];
P2 = new CHAR[20];
The area that is allocated and bytes is in the heap area.
strcpy (P1, "123456");//123456\\0 in a constant area, the compiler may optimize it with the
"123456" of P3 point//to a place.
}
comparison between 3.new/delete and Malloc/freeFrom a C + + perspective, allocating heap space using the new operator can call the class's constructor, and the malloc () function is just a function call, it does not call the constructor, and the parameter it accepts is a unsigned long type. Similarly, the delete operator calls the destructor before releasing the heap space, and the free () function does not.
Class time{public
: Time
(int,int,int,string);
~time () {
cout<<\ "call time\ ' s destructor by:\" <<name<<endl;
}
Private:
int hour;
int min;
int sec;
string name;
};
Time::time (int h,int m,int s,string n) {
hour=h;
min=m;
Sec=s;
Name=n;
Cout<<\ "Call Time\ ' s constructor by:\" <<name<<endl;
}
int main () {time
*t1;
t1= (time*) malloc (sizeof (time));
Free (t1);
Time *t2;
T2=new time (0,0,0,\ "t2\");
Delete T2;
System (\ "pause\");
return exit_success;
}
Results: Call time ' s constructor by:t2 calls time ' s destructor by:t2 from the result it can be seen that the constructor and destructor of the object can be called using New/delete, and the example calls a non- The default constructor. But when an array of objects is allocated on the heap, only the default constructor can be called, and no other constructors can be called.