malloc, Calloc, ReAlloc, new, and alloca function differences

Source: Internet
Author: User

malloc, Calloc, ReAlloc, new, and ALLOCA functions

I. Comprehensive narrative

1) malloc function: void *malloc (unsigned int size)

Allocates a contiguous space of size in the dynamic allocation area of memory, returns the first address of the allocated memory space if the allocation succeeds, otherwise returns NULL, and the requested memory is not initialized.

2) calloc function: void *calloc (unsigned int num, unsigned int size)

Allocates a contiguous space of num * size, according to the number of data given and the number of bytes that the data type occupies. after calloc requests memory space to the heap , it automatically initializes the memory space to 0, but malloc does not initialize and its memory space stores some random data.

3) realloc function: void *realloc (void *ptr, unsigned int size)

dynamically allocates a size memory space and assigns the first address of the memory space to PTR, which adjusts the PTR memory space to size. The memory space requested for the heap is not initialized.

4) New is an operator that dynamically allocates memory, automatically calculates the space to allocate, and when allocating memory space for a class type, invokes the class's constructor, initializing the memory space, which completes the initialization of the class. Dynamically assigned built-in types are automatically initialized depending on where the variable is defined, the variables defined outside the function body are initialized to 0, and the built-in type variables defined in the function body are not initialized. The memory space requested for the heap is not initialized for data of the built-in type, and the constructor initialization is called by default for data of non-built-in types.

5) function Alloca: It is possible to return a pointer to a storage unit of n contiguous characters, which the caller of the Alloc function can use to store a sequence of characters. request memory to the stack without manual release. Its invocation sequence is the same as malloc, but it allocates storage space on the stack frame of the current function, not in the heap. The advantage is that when the function returns, it automatically frees the stack frame it uses, so you don't have to worry about freeing up space. The disadvantage is that some systems cannot increase the stack frame length after the function has been called, and therefore cannot support the ALLOCA function. However, many packages use the Alloca function, and there are many systems that support it.


Standards in the second and C

ANSI C illustrates three functions for dynamic allocation of storage space
(1) malloc allocates a store for the specified number of bytes. The initial value in this store is not determined
(2) Calloc is an object of the specified length, allocating the storage space that can hold its specified number. Each bit (bit) in this space is initialized to 0
(3) ReAlloc change the length of the previously allocated area (increase or decrease). When you increase the length, you may want to move the contents of the previously allocated area to another area that is large enough, and the initial values in the new area are not determined


Iii. nuances and some key examples

The difference between malloc and calloc is 1 blocks from N blocks:
The malloc invocation form is (type *) malloc (size): Allocates a contiguous region of length "size" in the dynamic store of memory, returning the first address of the zone.
The Calloc invocation form is (type *) calloc (n,size): Allocates a contiguous region of length "size" bytes in the dynamic store of memory, returning the first address.
The ReAlloc invocation form is (type *) realloc (*ptr,size): Increases the PTR memory size to size. (You can also zoom out and shrink the content away ).

Another thing that cannot be directly seen is that malloc allocates memory and does not initialize the resulting memory, so the value will be random in a new piece of memory. Calloc automatically initializes the memory space to zero after the dynamic allocation of memory.

ReAlloc has a detail to note:

is to expand the existing piece of memory.

char* p = malloc (1024);
char* q = realloc (p,2048);

The question now is what we should do with pointer p. Just start with my most intuitive understanding, if it is directly p = NULL;. In the end, you just need to free up the Q space.

Because I'm doing a package recently. Results are found when doing unit tests. Sometimes I'm in free (q); Will make a mistake. So I was depressed.

After careful follow-up, found realloc after the Q and P's pointer address is the same. But sometimes it's not the same.

Carefully checked the information. Get the following information:

1. If the current contiguous block of memory is sufficiently realloc, just expand the space pointed to by P and return the pointer address of P. This time Q and P point to the same address.

2. If the current block of contiguous memory is not long enough, look for a longer enough place to allocate a new memory, q, and copy the contents of p to Q and return Q. and remove the memory space that P points to.

This means that the realloc sometimes produces a new memory address, and sometime does not. So after the assignment is complete. We need to determine if p is equal to Q. and do the corresponding treatment.

A bit of attention here is to avoid P = realloc (p,2048); This notation. It is possible that the memory address that P originally pointed to was lost after the realloc allocation failed.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

malloc, Calloc, ReAlloc, new, and alloca function differences

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.