C and C + + is how the dynamic allocation and release of memory, what is the difference?

Source: Internet
Author: User
Tags function prototype int size
C language provides dynamic allocation of memory functions are: malloc, Calloc, realloc, in the use of these functions must include its header files, respectively,:<malloc.h>, <stdlib.h>, <alloc.h>

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 assignment succeeds, or returns NULL, and the requested memory is not initialized.

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

Assigns a num * size contiguous space According to the number of data given and the number of bytes that the data type occupies.

When the memory space is calloc, the memory space is automatically initialized to 0, but the malloc is not initialized and its memory space stores some random data.

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

Dynamically allocate a memory space of size, and assign the first address of the memory space to PTR and adjust the PTR memory space to size.

The requested memory space is not initialized. The freed function is the free function:

Free function prototype is: void free (void *ptr)

Role: Frees the memory space requested by the above 3 functions.

Parameters: ptr: The first address that points to the memory space that needs to be freed.

In C + +, memory is divided into 5 areas, they are the heap, stack, free storage area, global/static storage and constant storage area.

Request and release the allocated storage space in the heap, using the two operators of new and delete respectively:

Pointer variable name = new type name (initialization);

Delete pointer name;

For example:

int *pi = new Int (0)

malloc and free are standard library functions for c++/c languages, and new/delete are the operators of C + +. Both can be used to request dynamic memory and free memory.

For non-intrinsic data type objects, Malloc/free cannot meet the requirements of dynamic objects. Objects are created with the constructor automatically executed, and the destructor is automatically executed before the object dies. Because Malloc/free is a library function and not an operator, it is not possible to impose the task of executing constructors and destructors on Malloc/free without the compiler controlling permissions.

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.