C and C + + is how to dynamically allocate and release memory, what is the difference

Source: Internet
Author: User

C language

The C language provides memory-dynamically allocated functions such as malloc, Calloc, realloc, which must contain their header files when using these functions, 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 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.

When calloc requests memory space, 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 requested memory space is not initialized.

The freed function is the free function:
The Free function prototype is:void free(void *ptr)

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

Parameter: ptr: Points to the first address of the memory space that needs to be freed.

C + + language

In C + +, memory is divided into 5 extents, namely heap, stack, free storage, global/static storage, and constant storage.

Apply 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)
Summarize

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

For non-intrinsic data type objects, light Malloc/free cannot satisfy the requirements of dynamic objects. Objects are automatically executed when they are created, and the object executes the destructor automatically before it dies. Because Malloc/free is a library function and not an operator, the task of executing constructors and destructors cannot be imposed on malloc/free, not within the control of the compiler.

C and C + + is how to dynamically allocate and release memory, what is the difference

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.