C language memory allocation (malloc/realloc/calloc difference)

Source: Internet
Author: User

Void * realloc (void * PTR, unsigned newsize );

Void * malloc (unsigned size); V

Oid * calloc (size_t nelem, size_t elsize); All in the stdlib. H function library.

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: the malloc call form is (type *) malloc (size ): allocate a continuous area with a length of "size" bytes in the dynamic storage area of the memory, and return the first address of the area.

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 and return the first address.

Realloc cannot guarantee that the allocated memory space is the same as the original memory space. The pointer returned by realloc may be directed to a new address.
Therefore, in the Code, the value returned by realloc must be re-assigned to P, for example:
P = (char *) realloc (p, old_size + new_size );
  
Even if you pass a null pointer (0) to realloc, then realloc is equivalent to malloc.
Int * P = (char *) realloc (0, old_size + new_size); // allocates a new memory space, which is equivalent to the following line: int * P = (char *) malloc (old_size + new_size );
  
Calloc (Len, size) is similar to malloc. The LEN parameter is the length of the element in the unit of the requested address, and the size is the number of elements, for example:
Char * P;
P = (char *) calloc (sizeof (char), 1000 );

 

 

First, let's look at the problematic program (the TC compiler is used here ):
# Include "stdlib. H "<br/> # include" stdio. H "<br/> void main () <br/>{< br/> int * I; <br/> I = (int *) malloc (sizeof (INT); <br/> * I = 1; <br/> * (I + 1) = 2; <br/> printf ("% x | % d/N", I, * I); <br/> printf ("% x | % d", I + 1, * (I + 1); <br/>}< br/>

The output result is:
8fc | 1
8fe | 2
This program is compiled and runs normally. If there is a problem, what is the problem?

First, a 2-size heap is created through malloc,
I points to 8fc and I + 1 points to 8fc + sizeof (INT) = 8fe.
However, the 8fe address is unprotected because it is not allocated to I + 1 by machines and will be occupied by other variables at any time.

The correct method is
# Include "stdlib. H "<br/> # include" stdio. H "<br/> void main () <br/>{< br/> int * I; <br/> I = (int *) malloc (sizeof (INT); <br/> * I = 1; <br/> I = (int *) realloc (I, 2 * sizeof (INT )); <br/> * (I + 1) = 2; <br/> printf ("% x | % d/N", I, * I ); <br/> printf ("% x | % d", I + 1, * (I + 1); <br/>}

Realloc can expand or contract the space indicated by the given pointer. Whether it is expansion or reduction, the content in the original memory will remain unchanged. Of course, for downgrading, the contents of the reduced part will be lost. Realloc does not guarantee that the adjusted memory space is the same as the original memory space. Instead, the pointer returned by realloc is likely to point to a new address.
Therefore, in the code, we must re-assign the value returned by realloc to P:
P = (int *) realloc (p, sizeof (INT) * 15 );

Even if you pass a null pointer (0) to realloc, then realloc is equivalent to malloc.
Int * P = (int *) realloc (0, sizeof (INT) * 10); // allocate a brand new memory space,

This line serves exactly the same purpose:
Int * P = (int *) malloc (sizeof (INT) * 10 );

"NOTE: In the TC compiler, sizeof (INT) = 2, and sizeof (INT) = 4 in VC;
The char type is the same in two compilers, both of which are 1 byte (8 bits )』

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

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/bdc995/archive/2008/11/13/3292205.aspx

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.