Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ C ++ free store and heap     - QEMU code analysis: BIOS loading process (Linux)

- Linux Mint 17 set up the Ruby environment (Linux)

- Linux pwd command learning experience (Linux)

- OpenSUSE / Linux network configuration (Linux)

- Using open source software to build XWiki Wiki system installed within the company (Linux)

- Create RAID 1 (mirroring) with two disks (Linux)

- Guide: Trickle restrict application bandwidth usage (Linux)

- Use PuTTY key authentication mechanism for remote login Linux (Linux)

- Ubuntu How to install screen recording tool Simple Screen Recorder 0.3.1 (Linux)

- 64-bit Windows Server 2012 R2 install Oracle 10g Second Edition (Database)

- Share useful bash aliases and functions (Linux)

- Bash environment is automatically install and initialize oh-my-zsh & autojump zsh (Linux)

- Ubuntu 15.10 under Python + Apache + CGI fully configured (Server)

- Understanding the Linux load average on other UNIX-Like systems (Linux)

- Ubuntu Thunderbird 24.4.0 (Linux)

- Linux in order to make NMAP hide and seek with the firewall (Linux)

- Use mysqldump backup performed MariaDB (Database)

- Install Firefox 28 on Ubuntu, Linux Mint (Linux)

- Linux server disk expansion and Oracle tablespace file migration operations (Database)

- Distributed Firewall Design on Linux platform (Linux)

  C ++ free store and heap
  Add Date : 2018-11-21      
  "Free store" VS "heap"

When I asked your C ++ memory layout, you would probably answer:

"In C ++, the memory area is divided into five areas, namely the heap, stack, free store, global / static storage area, the constant storage area."

If I then asked you free store and stack What is the difference, you might answer:

"Malloc a block of memory allocated on the heap, free use of free memory, while the new memory is applied on the free store, use delete to release."

This sounds true, but if I then asked:

Free store and heap are two distinct areas of memory it? They may do the same?

You may be senseless.

In fact, I see a lot of the Internet blog, free store division of the heap is the dividing line between new / delete and malloc / free. However, although the C ++ standard does not require, but many compilers new / delete are in malloc / free basis to achieve. So I ask: malloc so as to achieve new, the allocated memory on the heap or on the free store?

Technically, the heap (heap) is a C language and operating system terminology. Operating system stack is maintained by a special memory, which provides dynamic allocation function, when you run the program calls malloc () which will be allocated later may call free the memory return. The free store is a C ++ abstraction by new and delete objects dynamically allocate and release memory region through new applications may be referred to the free store. Basically, all the C ++ compiler default heap memory to achieve freedom, that is the default global operator new and delete might follow malloc and free way to be implemented by the new operator then allocated objects , it also heap, it is also on the free store correctly. However, programmers can also overload operators, use other free storage memory to achieve, such as global variables do object pooling, when the free store is distinguished from a heap. We need to remember is this:

Heap is the operating system maintains a memory, and free memory in C ++ new and delete through dynamic allocation and release of the object abstraction. Heap and the free store is not equivalent.

Source of problems

Then come back to take a look at the origin of the problem lies. The first time we use the C language, and there is no such controversy, it is clear to know malloc / free in the heap for memory operations. Until we see several free store (free store) in Bjarne Stroustrup's book, to be honest, I have been equivalent to the free store heap. In Herb Sutter's "exceptional C ++" expressly stated that the free store (free store) and heap (heap) is different. Discussions on the free store and stack are equivalent problems probably started from here:

Free Store
The free store is one of the two dynamic memory areas, allocated / freed by new / delete Object lifetime can be less than the time the storage is allocated;. That is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately deallocated. During the period when the storage is allocated but outside the object's lifetime, the storage may be accessed and manipulated through a void * but none of the proto-object's nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.

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 be implemented in terms of malloc and free by a particular compiler, the heap is not the same as free store and memory allocated in one area can not be safely deallocated in the other. Memory allocated from the heap can be used for objects of class type by placement-new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.

The authors also pointed out that the reason for the heap and the free store to come apart, because the C ++ standard draft about whether these two regions linked issues has been very careful not to give a detailed description and specific situations are new and delete according to malloc and free to achieve, or is put over the malloc and free are in accordance with the new and delete realized also inconclusive. Different mode of operation of these two memory areas, different access methods, so it should not be treated as the same thing to use.

in conclusion

Free store in C ++ by new and delete dynamic allocation and release objects abstraction, but Heap (heap) is a C language and operating system terminology, is the operating system maintains a dynamic memory allocation.

New memory area of ​​the application referred to in C ++ free store. With the heap implementation of free storage, it can be said that the application of new memory area on the heap.

Heap and free store there are differences, they are not equivalent.

If you come from the C language, C ++ never come into contact; or you start a free store concept familiar to C ++, but never heard of malloc C language, maybe you will not fall into the "free store like the same heap , it seemed different "such confusion among. It's like Bjarne Stroustrup said:

usually because they come from a different language background.
- Ubuntu users to install Gnome-Pie 0.6.5 (Linux)
- Use mdadm tool to create software RAID 0 (Linux)
- ORA-08102 errors (Database)
- grep command output highlighted word (Linux)
- Oracle 12C with multi-column index (Database)
- 5 interesting Linux command line tips (Linux)
- CentOS6 install Redis (Database)
- Repair after installing Ubuntu no boot device error (Linux)
- To install Samba server on CentOS 6.6 (Server)
- Linux environment variable settings and save places (Linux)
- Analysis of memory mapping process in Linux x86-32 mode (Linux)
- JavaScript closures and the scope chain (Programming)
- Ubuntu 14.04 compile, install, configure, the latest development version GoldenDict (Linux)
- GoldenGate update is missing (Database)
- Depth understanding of the TCP protocol (Database)
- Open SSH remote access service that allows Android phone Ubuntu 14.04 (Linux)
- What happens after the MySQL disk space is full (Database)
- The first IOS Objective-C program (Programming)
- Character Encoding notes: ASCII, Unicode and UTF-8 (Linux)
- Solaris 10 installation configuration mrtg monitoring system (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.