Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ free store and heap     - MySQL binlog group to submit XA (two-phase commit) (Database)

- To install PXE + Kickstart under CentOS 6.x operating system (Linux)

- Android memory optimization of the disk cache (Linux)

- Vmstat command Linux Performance Monitoring (Linux)

- The Samba service does not have permission to access (Server)

- SecureCRT remote connection Ubuntu fails to solve the case (Linux)

- Linux process or thread is bound to a CPU (Programming)

- How to make GRub instead of the default Ubuntu software center (Linux)

- Oracle Linux 5.5 (64bit) Install Oracle 11gR2 RAC detailed tutorial (Database)

- CentOS 6.6 compile and install phpMyAdmin configuration PostgreSQL9.4.1 (Database)

- rpm package specify the installation path (Linux)

- A key installation Gitlab 7 on RHEL6.4 and Setup Mail TX (Linux)

- Network security system (Network)

- AppCode developed Mac OS X application or shared library experience summary (Programming)

- How to install Hadoop on CentOS7 (Server)

- MySQL related statements (CRUD) (SQLyog software) (Database)

- shell script: the number of characters in the text to print no more than 6 words (Programming)

- Linux text processing tool of awk (Linux)

- How to protect your eyes automatically adjust the screen brightness on Linux (Linux)

- Custom Android UI template Comments (Programming)

 
         
  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.

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 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.
     
         
         
         
  More:      
 
- Oracle set and remove columns unavailable (Database)
- MySQL 5.6 master-slave replication configuration (Database)
- Let CentOS6 yum upgrade to support more source rpm package (Linux)
- Automatic Clear date directory shell script (Linux)
- Using Java program determines whether it is a leap year (Programming)
- Linux Nginx installation and configuration instructions (Server)
- Experts teach you safety precautions Linux Sniffer (Linux)
- GROUP BY extension (Database)
- Process safety monitoring and protection under the Linux operating system (Linux)
- Oracle delete table space prompted ORA-00604 and ORA-38301 (Database)
- Https (SSL / TLS) Detailed principles (Server)
- What is a logical partition management LVM, how to use in Ubuntu (Linux)
- IPTABLES configuration steps under Linux (Linux)
- Django how to generate content in non-HTML formats (Programming)
- About ORA-02391 solution (Database)
- Linux program analysis tool: ldd and nm (Linux)
- Upload the project to GitHub, synchronous remote repository Github (Linux)
- Oracle study notes view (Database)
- git checkout generated in the use of temporary br (Linux)
- Necessity in Java packages (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.