C language memory allocation function, Allocation Function

Source: Internet
Author: User

C language memory allocation function, Allocation Function

The C Language Standard Library provides three memory allocation functions, all of which are included in the header file <stdlib. h>.

 

1. malloc

Function prototype:

 

void *malloc( size_t size );

 

Parameters: Number of bytes to allocate memory size

Return Value: Pointer to the memory address. If an error occurs, NULL is returned.

Function: Allocate a memory space of the specified size.

Details: Memory spaceContinuousAndHeapSpace, need to useFree ()The function is manually released, and the space is junk data.

 

2. calloc

Function prototype:

 

void *calloc( size_t num, size_t size );

 

Parameters: 1. Number of memory to be allocated 2. number of bytes per memory

Return Value: Pointer to the memory address. If an error occurs, NULL is returned.

Function: Allocate a memory space of the specified size.

Details: Memory spaceContinuousAndHeapSpace, need to useFree ()The function is manually released, and the data in the space is initialized to 0.

 

3. realloc

Function prototype:

 

void *realloc( void *ptr, size_t size );

 

Parameters: 1. The object to change the space size 2. Change the original space size to size.

Return Value: Pointer to the memory address after the change. If the error is returned, NULL is returned.

Function: Change the size of an allocated space.

Details: Memory spaceContinuousAndHeapSpace, need to useFree ()Manually release the function.

The size can be larger than or smaller than the original size. When the size of the original space is greater than that of the original space, if there is sufficient memory allocation behind the original space, the size of the original space is expanded directly. Parameter 1 is equal to the return value.

If not, find another one that is large enough.ContinuousMemory Address, copy the content of the original address to the new address. Reclaim the original space and return the pointer of the new address.

 

Related Article

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.