Four memory areas (code area, global area, heap area and stack area)

Posted by gaugeboson on Mon, 07 Feb 2022 07:03:40 +0100

During the execution of C + + program, the memory is divided into four areas

·Code area: the binary code that stores the function body and is managed by the operating system.

·Global area: store global variables, static variables and constants

·Stack area: it is automatically allocated and released by the compiler to store the parameter values and local variables of the function.

·Heap area: it is allocated and released by the programmer. If the programmer does not release it, it will be recycled by the operating system after the program is completed.

1, Code area

① the code area stores the machine instructions executed by the CPU, which is the binary code compiled from the program.
② the code area is shared. For frequently executed programs, only one copy needs to be saved in memory.
③ the code area is read-only. The reason for reading only is to prevent the program from accidentally modifying the instruction.

2, Global area

        

The test code is as follows:

#include <iostream>
using namespace std;

//global variable
int g_a = 10;
int g_b = 10;

//Global constant
const int c_g_a = 10;
const int c_g_b = 10;

int main() {

	//local variable
	int a = 10;
	int b = 10;

	//Print address
	cout << "local variable a The address is: " << (int)&a << endl;
	cout << "local variable b The address is: " << (int)&b << endl;

	cout << "global variable g_a The address is: " << (int)&g_a << endl;
	cout << "global variable g_b The address is: " << (int)&g_b << endl;

	//Static variable
	static int s_a = 10;
	static int s_b = 10;

	cout << "Static variable s_a The address is: " << (int)&s_a << endl;
	cout << "Static variable s_b The address is: " << (int)&s_b << endl;

	cout << "The string constant address is: " << (int)&"hello world" << endl;
	cout << "The string constant address is: " << (int)&"hello world1" << endl;

	cout << "Global constant c_g_a The address is: " << (int)&c_g_a << endl;
	cout << "Global constant c_g_b The address is: " << (int)&c_g_b << endl;

	const int c_l_a = 10;
	const int c_l_b = 10;
	cout << "Local constant c_l_a The address is: " << (int)&c_l_a << endl;
	cout << "Local constant c_l_b The address is: " << (int)&c_l_b << endl;

	system("pause");

	return 0;
}

Result verification:
As can be seen from the figure, only the addresses of local variables and local constants are far away from other variables and are not in the global area.

III. stack area

It is automatically allocated and released by the compiler to store the parameter values and local variables of the function.
Note: do not return the address of the local variable. The address opened in the stack area is automatically released by the compiler.

Example code:

#include <iostream>
using namespace std;


char* get_str(void)
{
	char str[] = "abcdedsgads"; //str is in the stack area and character constants are in the global area
	printf("In the subfunction: str = %s\n", str);
	return str;
}

int main(void)
{
	char* p = NULL;

	p = get_str();
	printf("In the main function: p = %s\n", p);
	printf("\n");
	system("pause");
	return 0;
}

Function running result:

Why are the two printing results inconsistent? The reason is that after executing p = get_str(); After the function, the pointer variable p saves the first address of the array STR, but because the array is defined in the heap area, the constant value on the address has been released, so it is printed out in random code!!

4, Heap area

When the programmer releases the program, the operation ends if the programmer does not release the program.
Mainly use the new keyword to open up the heap area.

The example code is as follows:

#include <iostream>
using namespace std;


int* func()
{
	int* a = new int(10);
	return a;
}

int main(void) {

	int* p;
	p = func();

	cout << *p << endl;

	system("pause");

	return 0;
}

Function running result:

Firstly, the pointer variable a is defined in the stack area and the address of the heap data 10 is saved. Since 10 needs to be manually released by the programmer or the operating system in the heap area, p = func() is executed in the main function; The post pointer variable p always saves the address of 10, so it can be printed out!

Topics: C C++