"C Language Practice every day (ix)" Dynamic memory allocation

Source: Internet
Author: User

Introduction: The elements of an array are stored in contiguous locations in memory. When an array is declared. The memory it needs is allocated at compile time.

However, we can use dynamic memory allocation to allocate memory for it at execution time.


The life cycle of a piece of memory can be divided into four phases: allocation, initialization, use, and release.

Allocation of memory generally uses the malloc function (prototype: void *malloc (size_t size)) in the C function library.

There are a few things you should note about the malloc function:

1. The number ofmalloc is the number of bytes of memory that need to be allocated.

2. malloc allocates a contiguous amount of memory.

3, the allocation of success. Returns a pointer to the starting address of the allocated memory. Allocation failed with null pointer returned.

4. It is good practice to check every malloc return pointer.

Memory initialization is typically done manually prior to use, and can be completed at allocation time by the CALLOC function: Initialize the allocated memory to 0.

Use is the pointer to memory that is returned by the allocation.

The memory is freed to prevent memory leaks and is generally completed using the free function (prototype: void *pointer). Its parameters must be the values that were previously returned from malloc, Calloc, or realloc. Passing a null parameter to free has no effect whatsoever. Passing other parameters to free will make an error.

Note: there are several common dynamic memory errors

1. Forget to dereference the null pointer, that is, forget to infer the value returned by the allocation.

2. Cross the boundary when operating on allocated memory.

3. Releasing memory that is not dynamically allocated must be a pointer returned from malloc, Calloc, or realloc.

4. Attempt to release part of a dynamically allocated memory.

5, a piece of dynamic memory is released after the continued use.

A programming summary using dynamic memory:

1. Dynamic memory allocation helps eliminate limitations within the program.

2. Use sizeof to calculate the length of the data type and to increase the portability of the program.


Supplement: Calloc and ReAlloc functions

void *calloc (size_t nmemb, size_t size);

void *realloc (void *ptr, size_t size);

The difference between calloc and malloc one is that the former returns a pointer to memory before it is initialized to 0. The second is the number of calloc that contains the required elements and the number of bytes per element. Based on these values, you can calculate the total amount of memory that needs to be allocated.

The ReAlloc function is used to change the size of a previously allocated memory card. Use this function to make a chunk of memory larger or smaller. Assume that the original memory card cannot change size. ReAlloc will allocate a piece of memory of the correct size and copy the contents of the original piece of memory to the new block.

"C Language Practice every day (ix)" Dynamic memory allocation

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.