Differences and connections between malloc realloc and calloc in c language

  • 2020-04-02 01:15:31
  • OfStack

ANSI C illustrates three functions for dynamic allocation of storage space
(1) malloc allocates the storage area with the specified number of bytes. The initial value in this store is uncertain

(2) calloc is an object of specified length, and it allocates the storage space that can hold the specified number. Each bit in the space is initialized to 0

(3) the realloc   Change the length of the previously allocated area (increase or decrease). As you increase the length, you may need to move the contents of the previous allocation area to another sufficiently large area, and the initial value within the new area is uncertain

Realloc () realloc realloc() realloc realloc() realloc
Allows us to increase or decrease the length of the previous allocation area (the most common use is to increase the area).

If you allocate space for an array of length 512 and fill it at run time, but find that there is not enough space, you can call realloc to expand the storage space.

If there is sufficient space for expansion after the storage area, it can be extended to the higher address from the original storage area location and the same pointer value passed to it can be returned.

If there is not enough space after the original store, realloc allocates another store that is large enough to copy the contents of the existing array of 5, 1, and 2 elements into the newly allocated store.

Because this storage area may be moved, you should not use any Pointers to indicate that it is in that area.

Note that the last parameter to realloc is newsize(new length) of the storage area, not the difference between new and old lengths. As a special case, if PTR is a null pointer, realloc performs the same function as malloc and allocates a storage area of the specified length, newsize.

These allocation routines are typically implemented through SBRK (2) system calls. The system calls the heap of the process to be expanded (or shrunk). While SBRK can expand or shrink the storage space of a process, most implementations of malloc and free do not reduce the storage space of a process. The free space can be redistributed later, but it is kept in the malloc pool rather than returned to the kernel.

It should be noted that most implementations allocate slightly more storage space than required, with extra space for recording administrative information -- the length of the allocation block, a pointer to the next allocation block, and so on. This means that if you write the end of an assigned area, you will overwrite the management information for the next area. This type of error is catastrophic, but because it is not immediately apparent, it is difficult to detect.

Moving the pointer back to the allocated block may also overwrite the block's management information. Other potentially fatal errors are: releasing a block that has already been released; The pointer to call free is not the return value of the three alloc functions, etc. Because memory allocation errors are difficult to track, some systems provide an alternative implementation of these functions. Additional error checking is done each time any of the three allocation functions or free is called. This version of the function is available in the program by specifying a private library when the connection editor is invoked. There are also publicly available resources (such as those provided by 4.3+BSD) that use a special flag at compile time to allow additional runtime checks to take effect.

Because the operations of the storage space allocator are important to the runtime performance of some applications, some systems provide additional capabilities. For example, SVR4 provides a function called mallopt that allows a process to set variables and use them to control the operation of the storage allocator. You can also use another function called mallinfo to count the operations of the storage allocator. Check the malloc(3) man page of the system you are using to see if these features are available.

The alloca function
Another function worth mentioning is alloca. It is called in the same sequence as malloc, but it allocates storage on the current function's stack frame, not in the heap. The advantage is that when the function returns, the stack frame it USES is automatically freed, so you don't have to worry about freeing up space. The disadvantage is that some systems cannot increase the stack frame length after the function has been called, and therefore cannot support the alloca function. However, many software packages still use the alloca function, and many systems support it.


Related articles: