C language memory allocation: malloc, calloc, realloc

Source: Internet
Author: User

Everyone knowsProgramThe memory layout is divided into Stack, heap, static variable area, constant area, and so on. The space allocation on the stack is managed by the programmer, including the application and release of the space.

Here we will talk about several functions provided in the C standard library for operating space on the stack:

1. Void *Malloc(Unsigned size );

2. Void *Calloc(Size_t nelem, size_t elsize );

3. Void *Realloc(Void * PTR, unsigned newsize );

For malloc (unsigned size:

It tells the operating system to allocate a size space for it.

If the allocation is successful, the pointer pointing to the block memory is returned. If the allocation fails, a null pointer is returned. The program should control whether the allocation is successful.

When the allocation is successful, what is the memory allocated to it? If the memory has never been used, the memory is initialized to 0. If the memory has been used, the memory is the result of the previous use. Therefore, the content in this memory section is uncertain and should be controlled in the program. Normally, the memory section is set to 0 using the memset () function.

For calloc (size_t nelen, size_t elsize:

Through the interface, it is not difficult to find that this function tells the operating system to do this: Assign nelen blocks to me, and the size of each block is elsize.

Like malloc (), if the allocation is successful, a pointer pointing to the block memory is returned. If the allocation fails, a null pointer is returned. The program should control whether the allocation is successful.

Different from malloc (), the allocated memory is initialized to 0, which is the biggest difference.

For relloc (void * PTR, unsigned newsize:

Is to tell the operating system to allocate (may not actually allocate) A newsize segment of memory, the content of which is directed by PTR. Therefore, the following two situations may occur: If the PTR points to the memory length less than newsize, then the newsize space is re-allocated and the content pointed to by PTR is copied to the new space, the content at the end of the new space is uncertain (like malloc); If the PTR points to a memory length greater than newsize, the space will not be reassigned, however, the original space is truncated to newsize.

Finally, no matter which allocation method, free () should be used to release the memory after use; at the same time, pay attention to the pointer programming specifications, even if it is set to null at the time of definition or after release, it can avoid problems such as wild pointers. In actual programming, more use of malloc () and memset () memory Allocation; calloc () is more suitable for allocating space corresponding to the array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.