Chapter 4 Memory Allocation: malloc () and free () and calloc ()

Source: Internet
Author: User
Zookeeper

Malloc ():


It accepts a parameter, that is, the amount of memory bytes required. If it succeeds, the first address of the space is returned. The space is not initialized. If it fails, null is returned.

, But the memory found is anonymous)

(Allocation type *) malloc (number of allocation elements * sizeof (Allocation type ))

Example:

Double * PTB;

PTB = (double *) malloc (30 * sizeof (double ));

This code requests 30 double-type values and points PTB to the position of the space. Note that PTB is a pointer declaration pointing to a double-type value, instead of pointing to 30 data blocks of double type values. 30 of them can be a variable. This method is used when C90 cannot use a variable length array.

Free ():

Because the memory space found with malloc cannot be automatically released, a function to release the memory space will be used after calling malloc to prevent memory leakage.

Calloc ():

It is similar to the malloc function, but the calloc function accepts two parameters: the first is the size of the required memory, and the second is the size of each unit in bytes. It also sets all the bits of the memory block to 0. It can also use the free function to release the allocated memory.

Contact storage type and dynamic memory allocation:

From the ideal model, the program divides its available memory into three independent parts: a static variable with external links, internal links, and null links, and an automatic variable, the other is the dynamically allocated memory.

Static storage period: the amount of memory required for storing the variable is known at compilation. The data stored in this part is available throughout the program running until the end of the program.

Automatic variables are generated when the program enters the code that contains the variable definition. When the program exits this code, it terminates and processes this part as a stack, that is, in the memory, new variables are added in order when they are created, and removed in reverse order when they are eliminated.

The dynamically allocated memory is generated when the malloc function or related function is called. It is released when the free function is called, and the memory duration is controlled by the programmer instead of the system's fixed rules, in addition, the use of dynamic memory often leads to slower processes than the use of stack memory.





Chapter 4 Memory Allocation: malloc () and free () and calloc ()

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.