Semi-dynamic linux C memory management and linux memory management

Source: Internet
Author: User

Semi-dynamic linux C memory management and linux memory management

What is the first reaction when I see a semi-Dynamic Request for memory?

In fact, the semi-dynamic memory application is easy to understand. It is implemented using the alloca function in gnu c.

# Include <stdlib. h> void * alloca (size_t size)

The alloca function can dynamically apply for memory like malloc (note that the applied memory is located in the stack of the called function, and the page is automatically released after the function is called)

The advantage is obvious: You can freely apply for space without manually releasing the memory.

The downside is that when an error occurs, you cannot get a clear error message. In addition, non-GNU systems do not support alloca, which is a BSD extension with poor compatibility. (Other platforms may use C to write a simulated alloca, but the efficiency is not good)

In gnu c, you can replace alloca with an array of variable sizes.

Difference: an array of variable size is only valid in the region where the array name is located. If it exceeds the value, it is released. The reserved function ends when alloca applies.

Alloca can be used in the loop body. memory space is gradually increased in each loop. The variable-size array does not work.

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.