Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ free store and heap     - Java objects to garbage collection (Programming)

- CentOS 6.5 installation using a data recovery software extundelete (Linux)

- Linux system security configuration Collection (Linux)

- Network Security: SYN attacks against under linux (Linux)

- Bash Getopts - let your script supports command line parameters (Linux)

- Redis-- persistence articles (Database)

- Adjust the size of the hard disk VirtualBox (Linux)

- How to make a U disk to install Ubuntu (Linux)

- CentOS environment prepared crontab scheduled tasks (Linux)

- Google open source TCP team repaired the Linux kernel TCP flaw (Linux)

- Automated Password Generator: Linux under a special password generator (Linux)

- DataGuard the MRP can not start to analyze and solve problems (Database)

- Spacewalk remove packages install the update (Linux)

- In-depth summary of the PHP core of object-oriented (Programming)

- The basic principles for the protection of a good linux server security (Linux)

- Performance Optimization: Using Ramlog transfer log files to memory (Linux)

- MySQL Tutorial: Building MySQL Cluster under Linux (Database)

- AngularJS asynchronous service testing and Mocking (Programming)

- How to use the ps command to monitor progress in the implementation of Linux commands (Linux)

- Use Pylint regulate your Python code (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:      
 
- Git bulk delete remote tag (Linux)
- Ubuntu Froxlor Server Administration panel installation (Server)
- OS X CAOpenGLLayer how to enable OpenGL 3.2 core profile (Programming)
- Binary tree and some basic operations with binary list (Programming)
- Detailed usage history command (Linux)
- CentOS 6.5 installation Python3.0 (Linux)
- shell script error dirname: invalid option - b (Database)
- Binary Packages Golang (Linux)
- How to make a U disk to install Ubuntu (Linux)
- jdbc Oracle database connection string writing pluggable (Database)
- Bash common several configuration files (Linux)
- CentOS 7 How to install MySQL Server (Database)
- Install the Red Hat Container Development Kit on OSX (Server)
- Java Class file format parsing (Programming)
- The compiler installed Kaldi under Ubuntu 12.04 (Linux)
- Thinking in Java study notes - Generics (Programming)
- VMware virtual machine to install CentOS 7 (Linux)
- Oracle user password problem (Database)
- Linux Shell Scripting Interview Question (Linux)
- To compile and install Redis Linux and master-slave replication configuration (Database)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.