The use of dynamic memory in C and C ++ is different. To use dynamic memory in C, you must include a header file: # include <malloc. h>
Or # include <stdlib. h>
Then use the C language system function void * malloc (usigned
Size); to obtain the dynamically allocated memory. This function parameter is the number of bytes of the memory to be applied for and returns the first address of the requested memory, the returned memory type is void, so forced type conversion is required, such as int * array; array = (int
*) Malloc (sizeof (INT) * 10); in this way, the memory space of a 10 int type variable is dynamically applied.
However, in C ++, it seems that the statements used are simpler. the header file contained is # include <iostream. h>.
To apply for memory, you do not need to call the function as long as a keyword new. For example, int * array; array = new int [10]; note that int * array = new int (10 );
Instead of applying for 10 int-type spaces, we apply for an int-type memory space and assign it a value of 10. Of course, if int * A; A = new int;
Then we get a memory space that can store int-type data.
C and C ++ should be released before they exit the dynamic application. c uses the free () function and C ++ uses the delete keyword.