C language commonly used in several memory request function __ function

Source: Internet
Author: User

Several memory request functions commonly used in C language:

void* malloc (unsigned size); 
void* realloc (void* ptr, unsigned newsize); 
void* calloc (size_t numelements, size_t sizeofelement);

The above functions are defined in the header file Stdlib.h, so you must import the header file when you use it.

malloc:
The malloc function is the most commonly used in our usual writing code. The size of the parameter is the length of memory that needs to be requested, in bytes, or null if the request fails, or returns the first address of the request to the contiguous block of memory, which requires a pointer to the returned first address pointer to be cast to the target type. Such as:

Char *p = (char*) malloc (10);

realloc:
ReAlloc is the reallocation of memory to a pointer that has already been requested, that is, when a pointer to a block of memory is insufficient or redundant, you can use realloc to reallocate the appropriate size of memory, relative to the size of the memory block can be adjusted as needed. The parameter ptr is the original space address, and the newsize is the length of the address that is being applied again.
Such as:

Char *p = (char *) malloc (a);
p = realloc (P, 20);

Eventually p points to an address that may or may not change, but the size of the memory block that P points to is larger, and the contents of the original memory block of P are unchanged.
(the address that P points to may change after the memory is reassigned. Change is based on the actual situation, if the reallocation of memory than the original, and in memory before the original memory block followed by sufficient free space to allocate, then P's first address unchanged, If there is not enough space on the back of the original memory block to allocate, the system will look for a large enough free space elsewhere to allocate p, in which case the address that P points to will change, because either malloc or realloc, The block of memory it requests must be contiguous.

calloc:
Calloc is similar to malloc, but calloc more than malloc to do one step, is to apply to the memory block all initialized to 0. The parameter numelements represents the number of elements, sizeofelement represents the size of each element, that is, the size of the memory block the CALLOC requested is numelements * size_t bytes.

The above functions are all from the heap to apply for memory, so need to program apes themselves free memory, free memory:

Free (VOID*PTR);

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.