The difference between malloc and realloc

ANSI C describes three functions for dynamic allocation of storage space
(1) malloc allocates a specified number of bytes of storage. The initial value in this storage area is indeterminate
(2) calloc is an object of the specified length, and allocates the storage space that can hold the specified number of it. Each bit in this space is initialized to 0
(3) realloc changes the length (increase or decrease) of the previously allocated area. When increasing the length, it may be necessary to move the content of the previously allocated area to another area that is large enough, and the initial value in the newly added area is uncertain

. Realloc() is allocated when the function is allocated 
so that we can increase or decrease the previously allocated area The length of the zone (the most common usage is to increase the zone).

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

If there is enough space for expansion after the storage area, it can expand in the direction of the high address in the original storage area and return the same pointer value passed to it.

If there is not enough space after the original storage area, realloc allocates another storage area large enough to copy the contents of the existing 512-element array to the newly allocated storage area.

Since such a memory area may be moved, no pointers should be used to point to this area.

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

These allocation routines are usually implemented via the sbrk(2) system call. This system call expands (or shrinks) the process's heap. Although 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. Freed spaces are available for later reallocation, but keep them in the malloc pool and not return them to the kernel.

It should be noted that most implementations allocate slightly more storage than required, with the extra space used to record management information - the length of the allocated block, a pointer to the next allocated block, and so on. This means that if the end of an allocated area is written, the management information of the latter block will be overwritten. This type of error is catastrophic, but because it doesn't surface quickly, it's hard to spot.

Moving the pointer to the allocated block backward may also overwrite the management information for that block. Other potentially fatal errors are: freeing a block that has been freed; calling free with a pointer other than the return value of the three alloc functions, etc. Because memory allocation errors are difficult to track down, 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 can be used in the program by specifying a dedicated library when calling the link editor. There are also publicly available resources (such as those provided by 4.3+BSD) that are compiled with a special flag to enable additional runtime checks.

Because the operation of the storage allocator is very important to the runtime performance of some applications, some systems provide additional capabilities. For example, SVR4 provides a function called mallopt, which enables a process to set some variables and use them to control the operation of the storage allocator. Another function called mallinfo can also be used to count the operations of the storage allocator. Check the malloc(3) man page for your system to see if these functions are available.

.alloca function
There is another function worth mentioning, which is alloca. The call sequence is the same as malloc, but it allocates storage on the current function's stack frame, not on the heap. The advantage is that when the function returns, the stack frame it uses is automatically released, so you don't have to worry about freeing 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. Still, many packages use the alloca function, and many systems support it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325642366&siteId=291194637