"Reprint" C + + Free storage is equivalent to the heap

Source: Internet
Author: User

"Free Store" VS "heap"

When I ask you about the memory layout of C + +, you will probably answer:

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

If I continue to ask you what the difference is between a free storage area and a heap, you might answer:

"malloc allocates memory blocks on the heap, uses free to free up memory, and new requests the memory to be freed on a discretionary storage area using Delete." ”

It sounds right, but if I go on to ask:

are free storage areas and heaps two different areas of memory? Are they likely to be the same?

You may be ignorant.

In fact, I read a lot of blogs on the internet, dividing the dividing line between free storage and heap is new/delete and Malloc/free. However, although the C + + standard is not required, many compiler new/delete are implemented on a malloc/free basis. So ask: What is the application of the memory on the heap or in the free storage area by the new implementation of malloc?

Technically, heap is the term for the C language and the operating system. A heap is a special piece of memory maintained by the operating system that provides a dynamically allocated function that is allocated when the program calls malloc (), and later calls free to return memory. While free storage is an abstraction of the dynamic allocation and deallocation of objects through new and delete in C + +, the memory area requested by new can be called a free store. Basically, all C + + compilers use the heap for free storage By default, that is, the default global operator new and delete may be implemented as malloc and free, when the object allocated by the new operator is said to be on the heap, saying that it is also correct on a freely stored area. But programmers can also use other memory to implement free storage by overloading the operators, such as the pool of objects made of global variables, when the free store is different from the heap. All we need to remember is:

A heap is a piece of memory maintained by the operating system, and free storage is an abstraction of the dynamic allocation and deallocation of objects through new and delete in C + +. A heap is not equivalent to a free storage area.

The source of the problem

Go back and look at the origins of the problem. When we first used the C language, there was no such controversy, and it was clear that Malloc/free was doing memory operations on the heap. Until we saw the free store several times in Bjarne Stroustrup's books, to be honest, I kept the free store equal to the heap. In Herb Sutter's exceptional C + +, it is clear that the free store is different from the heap (heap). The question of whether the free storage area is equivalent to a heap is probably starting here:

Free Store
The free store is one of the areas, allocated/freed by New/delete. Object lifetime can be less than the time the storage is allocated; That's, free store objects can has memory allocated without being immediately initialized, and can be destroyed without The memory being immediately deallocated. During the period when the storage was allocated but outside the object ' s lifetime, the storage could be accessed and Manipul Ated through a void* but none of the Proto-object's nonstatic members or member functions could be accessed, with their addr Esses taken, or be otherwise manipulated.

Heap
The heap is the other dynamic memory area, allocated/freed by Malloc/free and their variants. Note that while the default global new and delete might is implemented in terms of malloc and free by a particular compile R, the heap is not a same as free store and a memory allocated in one area cannot be safely deallocated in the other. Memory allocated from the heap can is used for objects of class type by Placement-new construction and explicit destructio N. If so used, the notes on Free store object lifetime apply similarly here.

The authors also point out that the reason for separating the heap from free storage is that the question of whether there is a link between the two regions in the C + + standard has been carefully not given in detail, and that in certain cases new and delete are implemented in terms of malloc and freedom, or let it come. malloc and free are not conclusive as they are implemented by new and delete. These two memory areas operate in different ways and are accessed differently, so they should be used as something different.

Conclusion
    • Free storage is an abstraction of the dynamic allocation and deallocation of objects through new and delete in C + +, which is the term for C and operating systems, and is a dynamically allocated memory that is maintained by the operating system.

    • The area of memory requested by new is called a free store in C + +. With the free storage of the heap implementation, it can be said that the memory area requested by new is on the heap.

    • Heaps are still different from free storage, and they are not equivalent.

If you're from C, you've never been in C + +, or you've been familiar with C + + 's concept of free storage, and you've never heard of the C-language malloc, you might not be trapped in a "free storage area like a heap, as if it's different." It's like Bjarne Stroustrup said:

Usually because they come from a different language background.

It's probably just a different language background.

Reprint : http://www.linuxidc.com/Linux/2015-12/126520.htm

"Reprint" C + + Free storage is equivalent to the heap

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.