Differences between memory allocation functions such as malloc, calloc, and realloc

Source: Internet
Author: User

The three functions are declared as follows:
Void * realloc (void * PTR, unsigned newsize );
Void * malloc (unsigned size );
Void * calloc (size_t nelem, size_t elsize );
All in the stdlib. H function library

Their return values are all the addresses allocated by the request system. If the request fails, null is returned.

Malloc is used to apply for a new address. The parameter size is the length of the memory space, for example:
Char * P;
P = (char *) malloc (20 );

Calloc is similar to malloc. The elsize parameter is the unit element length of the request address, and nelem is the number of elements, for example:
Char * P;
P = (char *) calloc (20, sizeof (char ));
This example has the same effect as the previous one.

Realloc is to allocate space to a pointer that has allocated an address. The PTR parameter is the original space address, and newsize is the length of the requested address, for example:
Char * P;
P = (char *) malloc (sizeof (char) * 20 );
P = (char *) realloc (p, sizeof (char) * 40 );

Note that the space length here is measured in bytes.

 

C language standard memory allocation functions: malloc, calloc, realloc, etc.
The difference between malloc and calloc is the difference between 1 block and N block:
Malloc is called in the form of (type *) malloc (size): allocates a continuous area with a length of "size" bytes in the dynamic storage area of the memory, and returns the first address of the region.
Calloc is called in the form of (type *) calloc (n, size): In the dynamic storage area of the memory, allocate n consecutive areas with the length of "size" bytes, each bit is initialized to zero and returns the first address.
The realloc call format is (type *) realloc (* PTR, size): increase the size of the PTR memory to size.

Free is called in the form of free (void * PTR): releases the memory space pointed to by PTR.
C ++ is the new/delete function.

 

This article is from the csdn blog. For more information, see the source:Http://blog.csdn.net/Mobidogs/archive/2007/04/05/1552732.aspx

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.