# # C ++ base memory allocation functions: malloc, calloc, realloc, _alloca, new, free, delete

malloc:

void *malloc(size_t size);
  • Function: the length of the space in the application heap size bytes contiguous block of memory.
  • Parameters: size - size length byte space applications
  • Return Value: If successful distribution points are allocated memory pointer (initial value is undefined in this storage area) is returned that is the first application address space, otherwise it returns a null pointer NULL. When the memory is no longer used, should be used free () function to release the memory blocks. The function returns a pointer must be properly aligned, so that it can be used for any data objects.
  • Note: void * denote undetermined type of pointer, clearer say refers to the time of application memory space not know what type of data the user is using this space to store (such as a char or int or ...)
  • Working mechanism: in essence reflects the malloc function, it has an available memory block is connected to a long list of so-called free list . When calling malloc function, in which connection table to find a 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. Call 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. If no memory block to meet the requirements, function malloc returns a NULL pointer , so when the call malloc dynamic application memory block, the determination must return value.

calloc

void *calloc(unsigned n,unsigned size);
  • Function: the application heap num contiguous block of memory, the number of bytes of memory for each size; and the byte is set to 0 is initialized, the application returns the value of the first address space, more convenient application array, but efficiency ratio may malloc () will be slower, because multi-step initialization
  • Parameters: n- - contiguous blocks of memory, size - the number of bytes per block of memory
  • Return Value: If successful distribution points are allocated memory pointer (initial value is undefined in this storage area) is returned that is the first application address space, otherwise it returns a null pointer NULL. When the memory is no longer used, should be used free () function to release the memory blocks. The function returns a pointer must be properly aligned, so that it can be used for any data objects.
  • Note: void * denote undetermined type of pointer, clearer say refers to the time of application memory space not know what type of data the user is using this space to store (such as a char or int or ...)

realloc

void* realloc(void* memblock, size_t size);
  • Function: the pointer to first determine whether there is currently sufficient contiguous space, if any, to expand the address memblock points, and return memblock, if space is not enough, according to the size of allocation space designated size, the original data is copied to the beginning to end newly allocated memory area, then release the original memblock referred memory area, and returns the first address of the newly allocated memory area. I.e. reassign addresses of memory blocks.
  • Parameters: membloc - current memory block pointer,  size - specifies expanded
  • Return Value: If successful distribution points are allocated memory pointer (initial value is undefined in this storage area) is returned that is the first application address space, otherwise it returns a null pointer NULL. When the memory is no longer used, should be used free () function to release the memory blocks. The function returns a pointer must be properly aligned, so that it can be used for any data objects.
  • Note: void * denote undetermined type of pointer, clearer say refers to the time of application memory space not know what type of data the user is using this space to store (such as a char or int or ...)

 

 _alloca:

void * __cdecl alloca(size_t);

Memory allocation function, and malloc, calloc, realloc similar, but note an important difference, _alloca is to apply for space on the stack (stack), run out immediately released.

When calling alloca () function returns, and it will be automatically allocated memory is released. That is, the memory allocated by alloca local to some extent a function of `` stack frame "or context .alloca () does not have portability, and difficult to implement on a machine without a conventional stack when it is The return value can cause problems when passed directly to another function.

 fgets(alloca(100), 100, stdin)

For these reasons, alloca () substandard, unfit for use, no matter how useful it may be in a wide range of portable programs in it. Since C99 supports variable-length arrays (VLA), which can be used to better fulfill alloca () before the task. Deprecated

 

new/new[]

  • Function: the length of the space in the application heap for the current type (current type * array) of blocks of contiguous memory bytes.
  • Parameters: void
  • Return Value: If successful distribution points are allocated memory pointer (initial value is undefined in this storage area) is returned that is the first application address space, otherwise it returns a null pointer NULL. When the memory is no longer used, should be used delete delete [] function will release the memory block.
  • Note: new objects will be automatically call the constructor.

 

free

void free(void *ptr)
  • Function: release memory blocks.
  • Parameters: PTR - Pointer to a memory block to be deallocated, the memory block is performed before the allocation of memory by calling malloc, calloc or realloc of. If the parameter passed is a null pointer, then no action is taken.
  • Return value: void
  • Note: free () not to release space on the stack area, stack area space is managed by the OS, apply and release by the OS. After releasing the space required blank pointer, to avoid becoming a field guide

delete/delete[]

  • Function: release memory blocks.
  • Return value: void
  • Note: the use delete objects will be automatically call the destructor, an array of objects must use a delete [] to be released, the basic types of spatial arrays may delete / delete [] to be released. delete can not go to free up space in the stack area, stack area space is managed by the OS, apply and release by the OS. After releasing the space required blank pointer, to avoid becoming a field guide
Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/105094625