calloc ( ), malloc (), realloc (), Free ()void *calloc (size_t nobj, size_t size); Allocate enough memory to an array of Nobj objects of size, and return a pointer to the first byte of the assigned region; Returns NULL if there is not enough memory. The initialization size of the space is 0 bytes.char *p = (char *) calloc (sizeof (char)); void *malloc (size_t size); Allocate enough memory to the object of size, and return a pointer to the first byte of the assigned region; Returns NULL if there is not enough memory. The allocated space is not initialized. Char *p = (char *) malloc (sizeof (char)); void *realloc (void *p, size_t size); Change the size of the object that P points to to the amount of size byte. If the newly allocated memory is larger than the original memory, the contents of the original memory remain unchanged and the increased space is not initialized. If the newly allocated memory is smaller than the original memory, the new memory retains the contents of the original memory and the increased space is not initialized. Returns a pointer to the new allocated space; If there is not enough memory, then return NULL, the original P point to the memory area unchanged. Char *p = (char *) malloc (sizeof (char)); p= (char *) realloc (P, 256); void free (void *p); Releases the memory space that p points to; When P is null, it does not work. P must first call calloc, malloc or realloc. Free (p); About ReAllocprototype: extern void *realloc (void *mem_address, unsigned int newsize);usage: #include <stdlib.h> some compilers need #include <alloc.h>function: Change the size of the memory area referred to mem_address as newsize length. Note: Returns a pointer to the allocated memory if the reallocation succeeds, or null pointer return. When memory is no longer in use, you should use the free () function to release the memory block. Note: The data in the original memory remains unchanged here. For Example://REALLOC.C#include <syslib.h> #include <alloc.h>Main () { char *p;CLRSCR ();// Clear Screenp= (char *) malloc (100); if (p) printf ("Memory allocated at:%x", p); Else printf ("Not Enough memory!/n");GetChar ();p= (char *) realloc (p,256); if (p) printf ("Memory reallocated at:%x", p); Else printf ("Not Enough memory!/n");Free (p);GetChar (); return 0; }detailed description and attention points: 1. If there is enough space to enlarge the block of memory pointed by Mem_address, allocate additional memory and return mem_address "Expansion", as we know, is that realloc allocates memory from the heap, and when a chunk of memory is enlarged, realloc () tries to get additional bytes directly from those bytes behind the existing data on the heap, and if it can be satisfied, it will be all right. In other words, if there is enough free space behind the original memory size to allocate, plus the original space size = newsize. OK, then. Get a piece of contiguous memory. 2, if the original memory size does not have enough free space to allocate, then from the heap to find another newsize size of memory. and copy the contents of the original size memory space into the newsize. Returns a new mem_address pointer. (The data was moved). The old block was put back on the heap. For example: #include <malloc.h> Char *p,*q; p = (char *) malloc (10); Q=p; p = (char *) realloc (p,20); .............................. This program may not be able to pass through the compiler, because the compiler may eliminate some pitfalls for us. Here we just added a record of the original memory address of the pointer Q, and then recorded the original memory address p, if unfortunately, the data has moved, then the recorded memory address Q point to the memory space is actually put back on the heap! We should finally be aware of the problem and the horror. 3. Return situation Returns a pointer to a void type, and the call succeeds. (This will be forced type conversion again when you need it) Returns NULL when the size (the second argument) that needs to be expanded is 0 and the first argument is not NULL, at which point the original memory becomes "Freed (free)". Returns NULL, where the size of the original memory space remains unchanged when there is not enough space to expand. 4. Special circumstances If Mem_address is Nell, then ReAlloc () and malloc () are similar. Allocates a newsize block of memory, returning a pointer to the memory block. If the newsize size is 0, then the mem_address pointing memory is freed and Null is returned. Returns NULL if there is not enough memory available to complete the reassignment (expanding the original memory block or allocating a new memory block). And the original memory block remains unchanged.
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