C and C ++ application similarities and differences between dynamic memory space

C and C ++ application similarities and differences between dynamic memory space

The relationship between C and C ++ language, we can describe this, C ++ inherited from the C language, but both have their own unique features, such as a significant difference in how to apply dynamic memory space.

First of all we want to know why the need to dynamically allocate memory space? 

Dynamic allocation corresponding static allocation, in the computer memory into the stack area and heap area, stack area where the space relative to the stack area will be much smaller, can be understood as, their homes and warehouses.

Static allocation is to allocate space for a fixed-size stack area in advance, and dynamic allocation is how much how much allocation should be noted that, programmers need to maintain the dynamic allocation of memory.

 

He said the general, respectively, we look at the difference between the dynamic allocation of memory space in C and C ++ now:

The following is a C code style dynamic allocation language:

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main(){
 4     int *p;
 5     p = (int*)malloc(sizeof(int));
 6     *p = 4;
 7     printf("%d\n",*p);
 8     free(p);
 9     return 0;
10 }

 

 C language called dynamic allocation of memory requires a malloc () function, and free () function is used to free the memory, the two functions need to import header file "stdlib.h".

void * malloc function defaults to the type, the size of its parameters of the application space, if desired return address malloc function, it is necessary to specify the type strongly turned, in the above code, the need to match the pointer p type, using (int * ) strong turn,

free function to release memory. Its parameters need to free up space for the pointer. Note: Be sure to free up memory, or will cause waste of space.

 

The following is a dynamically allocated memory C ++ style code:

 1 #include <iostream>
 2 using namespace std;
 3 int main(){
 4     int *p1  = new int;
 5     *p1 = 4;
 6     cout<<*p1<<endl;
 7     
 8     int *p = new int[3];
 9     p[0] = 1;
10     p[1]=2;
11     p[2] = 3;
12     p[3] = 4;
13     cout<<p[0]<<" "<<p[1]<<" "<<p[2]<<" "<<p[3]<<endl;
14     
15     delete p1;
16     delete []p;
17     return 0;
18 }

 

 Dynamic memory allocation in C ++ header files do not need to import, and delete it with a new two keywords for the exercise of this function, the code above to emphasize the difference between constant and array, the biggest difference is the way to free up memory, C ++ allocates memory It has the advantage that it is no longer necessary to calculate the size of the data type, i.e. not need to use sizeof function, relatively speaking, to be convenient.

 

Well, that's the way of dynamically allocated memory between said today, C language and C ++ respectively, we want to have a little help.

If wrong, please point out, it must be corrected.

Guess you like

Origin www.cnblogs.com/whtmomo/p/11334494.html