Code four areas

The code memory is mainly divided into four sections

  1. Code area
  2. Global area
  3. Stack area
  4. Heap area

The code area and global area are before running, and the stack area and heap area are after running. That is, if the exe file does not run, there is no stack area and heap area.

1. Code area

That is, the code after programming the exe file

Features:

  • Only one

      Because it is impossible to run the program once, copy all the code again, so there is only one copy of the code area

  • Confidentiality

      After the program becomes an exe file, do not check the source code of the exe file

2. Global area

  • Global variable
  • static variable
  • Constants (note that only global variables are the global area)

The reason why constants need attention is because although local constants seem to be unable to be modified, they can be modified by pointers to the address, so local constants can be modified, so the address of local constants is not in the global area

Look at the code directly:

#include<iostream>
using namespace std;

int a = 10;
int aa = 20;

const int d = 10;
const int dd = 20;

int main()
{
	int b = 10;
	int bb = 20;

	static int c = 10;
	static int cc = 20;

	const int e = 10;
	const int ee = 20;

	cout << "全局变量  " << int(&a) << endl;
	cout << "全局变量  " << int(&aa) << endl << endl;


	cout << "局部变量  " << int(&b) << endl;
	cout << "局部变量  " << int(&bb) << endl << endl;


	cout << "静态变量  " << int(&c) << endl;
	cout << "静态变量  " << int(&cc) << endl << endl;

	cout << "全局常量  " << int(&d) << endl;
	cout << "全局常量  " << int(&dd) << endl << endl;


	cout << "局部常量  " << int(&e) << endl;
	cout << "局部常量  " << int(&ee) << endl;

	system("pause");
	return 0;
}

It can be seen that the addresses of local constants and local variables are relatively close, while the other three are relatively close

3. Stack area

Features:

  • Automatic application, automatic release, compiler management

      There are mainly local parameters, parameters in the function

4. Heap area

  • Manual application, manual release, programmer management
  • If you don’t release it, the compiler will release it for you

       Mainly new objects

Guess you like

Origin blog.csdn.net/qq_46423166/article/details/112299965