hello, I'm meeting you again ! This time I bring learning sharing is ： Clear explanation c/c++ Memory mode of program – Four memory areas . First of all, let's explain ,“ Clear explanation ” It's not about the four internal memory areas , But to tell you what the four memory areas are , Its basic knowledge , Let's meet it in our daily study, and we won't be at a loss . Four areas of learning memory are also of great help to language learning （ My personal experience , I didn't understand before “ This code has to be written like this , That can't be written like that ” Such regulations , Now suddenly I understand , It has something to do with four memory areas !）, I believe you will feel the same after reading this blog .
<> Contents of this article
1. Memory partition model
2. Code area
3. Global area
4. Stack area
5. Heap area
<>1. Memory partition model
c/c++ When the program is executing , The memory is roughly divided into four areas
* Code area ： The body code of binary function , Managed by operations
* Global area ： Store global variables, static variables and constants
* Stack area ： Automatically assigned and released by the compiler , Store the parameter value of the function
* Heap area ： Assigned and released by the programmer , If the programmer does not release , The program is recycled by the operating system at the end of the program
The meaning of four memory areas
Data stored in different areas have different life cycles , Give us more flexible programming
Here is a brief introduction to the memory distribution model , Now let's take a concrete look at their respective functions
<>2. Code area
deposit CPU Machine instructions executed
The code area is shared , The purpose of sharing is for frequently executed programs , Just have a copy of the code in memory
The code area is read-only , The reason for making it read-only is to prevent the program from accidentally modifying its instructions
<>3. Global area
Global and static variables are stored here
The global area also includes the constant area , String constants and other constants are also stored here
The data in this area is released by the operating system at the end of the program
In order to better understand the characteristics of the data in the global area and the data not in the global area , I use a code image to show you , I believe you can see it at a glance .
（ For the convenience of observation , All addresses are used int A forced turn ） As like as two peas, we can see clearly that the addresses of some data are identical at the beginning. , Then they are stored in the same area . Some of the beginnings are different , They are stored in different areas . For example, global variables , Static variables , string constant ,const Modify the global constant variables, which are stored in the global area , And local variables ,const Modified local variables , They are stored in other areas .
In order to facilitate you to remember those data in the global area , I drew a picture for you , I made a comparison
<>4. Stack area
Automatically allocated and released by the compiler , Store the parameter value of the function , Local variables, etc
matters needing attention ： Do not return the address of a local variable , The address of stack area is released automatically by compiler
Why not return the address of a local variable ? Now let me tell you with a code diagram
You'll find out , Output the same code twice , Why are the output values different ? That's strange ?
In fact, at the end of the function , data a He was released , Using dereference operator * To operate that memory , That memory doesn't belong to you anymore , So it will output garbled code . But for the first time, it's obvious that 10 ah , In fact, this is VS A reservation made by the compiler , I'm afraid you deleted some data by mistake , He made a reservation , So the first output is 10.
<>5. Heap area
Assign release by programmer , If the programmer does not release , The program is recycled by the operating system at the end of the program
stay c Use in language malloc Open up memory in heap area ,c++ of use new
It's used below new Explain
The same as usual , Let's start with the previous code diagram
Here is an additional illustration , Pointers are local variables in nature , Put it on the stack , The data saved by the pointer is placed in the heap area
Back to figure 1 , You'll find that now you can get the duplicate output 10. Why , because 10 This data is stored in the heap , As long as you don't release him or end the program , It's always there . Isn't it amazing , Ha ha ha .
This blog is over here , On the contents of the four regions , It's very shallow , But at the beginning of learning, it's enough to understand this knowledge . I hope this sharing can help you , If you find something wrong in your blog , Welcome to point out in the comments area . I'll see you next time !