Usage and significance of C language malloc

1, header malloc () function is stdlib.h, a function of the following statement:

void* malloc(size_t size);

Wherein the parameter size_t size represents the size of dynamic memory allocation space, in bytes.

typedef size_t is redefined redefined role is to make this type of data user at a glance, the user indicates the parameter represents a length, in the size plus t, is represented by the integer data type, see later xxx_t type, integer data types are usually redefined.

Here return malloc () function is a pointer value, or a first address of the memory space allocated

If malloc () function for the success of some memory space is returned first address fails returns NULL

2、int *p;

p = malloc(sizeof(int));

Here is not written, error: invalid from the type 'void *' to type 'int' conversion should read:

p =(int *) malloc(sizeof(int));

3, prior to the use of space malloc () function application, preferably with memset () function to clean up this memory space, because using malloc () function for the space of only guarantee is that the size of the memory space, does not guarantee memory whether there is space junk data

4, when the space is not in use malloc () function application, you should use the following function to free memory space out of this:

void free (void * ptr); void * ptr is where malloc () function's return value, which is the first address of the memory space

If you only know to use, do not know the release, then the embedded 7 days * 24 hours of operation, it is easy memory leak occurs, eventually leading to paralysis of the system

5, malloc () function allocates space dynamic programming: / ************************************** *

malloc () function allocates space dynamic programming:

(1) Define a type char * pointer variable p

(2) p-allocate 10 bytes of memory space

(3) Copy "come on" string to the memory space pointed to by p

(4) A p pointer to memory space and then expand the 20 bytes

(5) ", baby!" Character copy space behind the string pointed to by p

(6) the release of space pointed to by p

****************************************/

#include

#include

#include

using namespace std;

int main ()

{

char *p;

p = (char )malloc(10sizeof(char));memset(p,0,10*sizeof(char));strcpy(p,“come on”);

cout << "p: " << p << endl;p =(char )realloc(p,20sizeof(char));

cout << “p: " << sizeof§ << endl;strcat(p,”,baby!");

cout << "p: " << p << endl;free§;

return 0;} Output: p: come on

p: 4

p: come on,baby!

malloc Note:

After 1 malloc memory in addition to free until the release can also be automatically released until the end of the program run

Process 2 program is running, if you have been lead to malloc memory without releasing this memory can not be used again, that memory leaks. This way, the application will be gradually used up all available memory and eventually causes the application to crash because of insufficient memory stop.

3 So a good habit is that we must remember that after malloc free of.

Published 240 original articles · won praise 3 · Views 3178

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105163749