Introduction to Dynamic memory

Source: Internet
Author: User

Why use dynamic memory?

usually when we declare an array, we have to specify his length, but the length of the array is only known when the program is running, because the amount of memory he needs depends on the type of input data. Although this method is simple, but his shortcomings have greatly limited the flexibility and robustness of the program. This approach has been artificially restricted, but when we enter data elements that exceed the length of the array, he cannot handle the situation , so we usually set the length of the array to very large. Because the array is large, but if we enter a few data elements, it is a great waste of memory space . The most important thing is that if we enter an element that exceeds the length of the array, some compilers will not give an error, he will keep the array full, and the remaining elements will not be accessed, thus causing the program to output an incorrect value.

So sometimes we want to use dynamic memory.

Dynamic memory allocation is primarily intended to resolve memory allocations for arrays that know the required memory space at run time .

Let's begin by understanding the four functions of Malloc,free,calloa,realloc:

"malloc", function prototype void *malloc (size_t size);

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m02/7d/44/wkiol1bkfc7bo8ebaacdqc7pjie744.png "title=" Malloc.png "alt=" Wkiol1bkfc7bo8ebaacdqc7pjie744.png "/>


"Calloc", function prototype void *calloc (size_t num elements,size_t Element_size)

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/7d/46/wkiom1bke8lgopemaabuhqpdvqy048.png "title=" Calloc.png "alt=" Wkiom1bke8lgopemaabuhqpdvqy048.png "/>

"ReAlloc" function prototype void *realloc (void *ptr,size_t new_size)

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m01/7d/46/wkiom1bke9xdgiyhaac6u0bsqwa744.png "title=" Realloc.png "alt=" Wkiom1bke9xdgiyhaac6u0bsqwa744.png "/>

"Free" function prototype free (p);

The argument for free must be null, or the return value of Malloc,calloc,realloc.

His role is to free up memory (must be freed after memory is exhausted, or memory leaks will occur, with less memory). Of course, when you're done releasing p still points to the starting position of the memory, so also manually assign the address p to null, namely: P=null.

When using free, make sure that the address of the freed memory is no longer being accessed.


Common errors for dynamic memory usage:

1. Failed to check if memory matches successfully

2. Out of bounds when operating memory

For example : Request an array arr[3], if the array reference is less than 0 or subscript is greater than 2 will occur out of bounds.

3. When free memory is used, the address of the freed memory is still accessed.


This article from the "11132019" blog, reproduced please contact the author!

Introduction to Dynamic memory

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.