C language malloc, free usage

First, what is a pointer: pointer is used to store the address of a variable, which is the operation of the operating memory. C language definition of a variable stored in the memory address pointer, which is a 32-bit unsigned integer value.

1, how to initialize a pointer

int * pbuff1; and int * pbuff2 = NULL; what difference is it

First, pbuff1 not initialized point to NULL, did not point to valid memory. So pbuuf1 is a field guide. It will point to a random address, which is very dangerous. When you write to the field guide, no one will know what the result may modify the program out of other values.

pbuff2 initialized to NULL pointer indicates pbuff2 does not point to anything, the pointer vacant. Pointer chaos will not stretch the finger.

2, how to use a pointer

Former recommended pointer to the allocated space. Generally, when using pointers you need to know how much of the space.

This use malloc:

void * malloc (long NumBytes): This function NumBytes bytes allocated, and returns a pointer pointing to this memory. If the allocation fails, it returns a null pointer (NULL)

pBuff = malloc(lenAll*sizeof(char));

if(pBuff!=NULL)

{

memcpy(pBuff,buff,8*sizeof(char));

}

Here it is to allocate a space, and then the memory address pointed pBuff, if the allocation is successful, put the received value is stored in the buff to pBuff pointer.

Also initialized unsigned char * pBuff = NULL;

3, how to prevent memory leaks

malloc corresponding free:

void free (void * FirstByte): This function is before the space allocated by malloc returned to the program or operating system, which is released this memory, let it regain freedom.

Note free in the end what is released:

free () is a pointer to the released memory. Pointer variable still exists, only to be destroyed until the end of the program. But now the pointer is undefined content of garbage, so now pointer has become a field guide.

Thus, after the release of the memory pointer to NULL, vacant. Prevent accidentally pointer dereference has been in the back.

At the same time, it does not confirm this memory in the free, not free then finished using.

4, added:

malloc and free is one to one, if you malloc only once, and the corresponding necessary free time, repeated release is wrong.

malloc () function is working mechanism:

In essence reflected malloc function, it has a connection of the available memory blocks into a long list of so-called free list. When calling malloc function, it looks in a connection table memory block large enough to satisfy the user request is required. Then, the memory block is divided into two (equal to the size of a size requested by the user, the other one is the size of the remaining bytes). Next, the user will be assigned to that memory passed to the user, and the remaining piece (if any) to return to the connection table. When calling free function, the user will release the memory blocks are connected to the free chain. In the end, the idle chain will be cut into many small fragments of memory, if the user then apply for a large segment of memory, then the idle chain fragment may not meet the requirements of the user. Thus, malloc function request delay, and starts to check each of the memory segments rummaging on the idle strand, sort them, the adjacent small free blocks into a larger block of memory.

Published 241 original articles · won praise 3 · Views 3184

Guess you like

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