C + + memory partition model

When the C + + program is executed, 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 at the end of the program

The meaning of four memory areas: different areas store different data, give different life cycles, and be more flexible.

  • The code area and global area exist before the program runs, while the stack area and heap area appear after the program runs.

Code area:
It is used to store the machine instructions executed by the CPU (the area of binary code). All the codes written by yourself are stored in the binary format
characteristic:
The code area is shared (for example, if we run an exe file, we can run it multiple times at the same time without copying)
The code area is read-only (to prevent the program from accidentally modifying the data in it)

Global area:
It is used to store global variables, static variables and constants (which will be described below)

The variables written into the function body are local variables stored in the stack area;
Variables written outside the function body are global variables and stored in the global area;

1, Let's take a look at local variables. Local variables are not stored in the global area.

	int a = 10;
	int b = 10;
	cout << "local variable a Your address is:" << (int)&a << endl;
	cout << "local variable b Your address is:" << (int)&b << endl;


2, Then let's look at global variables:

#include<iostream>
using namespace std;
//global variable
int a_a = 10;
int a_b = 10;
int main()
{
	//Global area
	//Global variable, static variable, constant

	//Create a normal local variable

	int a = 10;
	int b = 10;
	cout << "local variable a Your address is:" << (int)&a << endl;
	cout << "local variable b Your address is:" << (int)&b << endl;

	cout << "global variable a_a Your address is:" << (int)&a_a << endl;
	cout << "global variable a_b Your address is:" << (int)&a_b << endl;


	system("pause");
	return 0;
}


It can be seen that local variables and global variables are too different and are not in the same data segment.

3, Let's look at static variables:

Add two static variables under the above code and find their addresses:

	static int a_c = 10;
	static int a_d = 10;
	cout << "Static variable a_c Your address is:" << (int)&a_c << endl;
	cout << "Static variable a_d Your address is:" << (int)&a_d << endl;


It can be seen that global variables and static variables are in the same data segment, and they are all in the global area;

There are also constants:
1. String constant:

//1. String constant
	cout << "The address of the string constant is:" << (int)&"hello world" << endl;


It can be seen that the string constant also belongs to the global area, but it may be a little away from the positions of global variables and static variables;

2.const modified global variable:

Based on the above, add two const modified global variables

#include<iostream>
using namespace std;
//global variable
int a_a = 10;
int a_b = 10;
const int b_a = 10;
const int b_b = 10;

int main()
{
	//Global area
	//Global variable, static variable, constant

	//Create a normal local variable

	int a = 10;
	int b = 10;
	cout << "local variable a Your address is:" << (int)&a << endl;
	cout << "local variable b Your address is:" << (int)&b << endl;

	cout << "global variable a_a Your address is:" << (int)&a_a << endl;
	cout << "global variable a_b Your address is:" << (int)&a_b << endl;

	//Static variables add static before ordinary variables
	static int a_c = 10;
	static int a_d = 10;
	cout << "Static variable a_c Your address is:" << (int)&a_c << endl;
	cout << "Static variable a_d Your address is:" << (int)&a_d << endl;

	//constant
	//1. String constant
	cout << "The address of the string constant is:" << (int)&"hello world" << endl;

	//2. Dest modified global variable
	cout << "const Modified global variable b_a the address is:" << (int)&b_a << endl;
	cout << "const Modified global variable b_b the address is:" << (int)&b_b << endl;

	system("pause");
	return 0;
}


It can be seen that it is still in the same data segment, with only a slight gap;

3.const modified local variables:

Two const modified local variables are still defined on the basis of the above code:

#include<iostream>
using namespace std;
//global variable
int a_a = 10;
int a_b = 10;
const int b_a = 10;
const int b_b = 10;

int main()
{
	//Global area
	//Global variable, static variable, constant

	//Create a normal local variable

	int a = 10;
	int b = 10;
	cout << "local variable a Your address is:" << (int)&a << endl;
	cout << "local variable b Your address is:" << (int)&b << endl;

	cout << "global variable a_a Your address is:" << (int)&a_a << endl;
	cout << "global variable a_b Your address is:" << (int)&a_b << endl;

	//Static variables add static before ordinary variables
	static int a_c = 10;
	static int a_d = 10;
	cout << "Static variable a_c Your address is:" << (int)&a_c << endl;
	cout << "Static variable a_d Your address is:" << (int)&a_d << endl;

	//constant
	//1. String constant
	cout << "The address of the string constant is:" << (int)&"hello world" << endl;

	//2. Dest modified global variable
	cout << "const Modified global variable b_a the address is:" << (int)&b_a << endl;
	cout << "const Modified global variable b_b the address is:" << (int)&b_b << endl;

	//3.const modified local variable
	const int c_a = 10;
	const int c_b = 10;
	cout << "const Modified local variable c_a Your address is:" << (int)&c_a <<endl;
	cout << "const Modified local variable c_b Your address is:" << (int)&c_b << endl;


	system("pause");
	return 0;
}


Here we find a big gap. There is a big gap between the address of const modified local variables and ordinary global variables.
const cannot change the local word.

Therefore, it is concluded that:
Whether the const area is modified locally or not; All global variables are in the global area, regardless of whether they are modified by const (global constant); String constants and static variables are in the global area, as shown in the figure:

The first two areas exist before the program runs, and there is a stack area after the program runs.

4, Stack area

  • It is automatically allocated and released by the compiler to store the parameter values and local variables of the function
  • Do not return the address of the local variable. The data opened up in the stack area is automatically released by the compiler

Example:

#include<iostream>

using namespace std;
int* fun()
{
	int a = 10;//Local variables are stored in the stack area
	return &a;//Returns the address of a local variable


}
int main()
{
	int* p = fun();
	cout << *p << endl;//The result of the first normal operation
	cout << *p << endl;//An error occurred

	system("pause");
	return 0;
}


Therefore, the address of the local variable cannot be returned in the function. Although the first operation is OK and can run the correct result, an error will appear the second time. The space opened up by the stack has been automatically released by the compiler.

Formal parameters and local variables are stored in the stack area.

5, 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 at the end of the program. In C + +, new is mainly used to open up memory in the heap area

Example:

#include<iostream>
using namespace std;
int* func()
{

	//Using the new keyword, you can open up the heap area
	int* p = new int(10);//*p is actually a local variable, which is stored on the stack, but its address is to the address on the heap
	return p;
}
int main()
{
	int* p = func();

	cout << *p << endl;
	cout << *p << endl;
	cout << *p << endl;

	system("pause");
	return 0;
}

6, new operator

1. As we know above, the data in the heap area is managed, developed and released by the programmer. If you want to release it, use the keyword delete;

int* func()
{
	int* p = new int(10);
	return p;
}
void test1()
{
	int* p = func();
	cout << *p << endl;
	cout << *p << endl;

}


After adding delete

#include<iostream>
using namespace std;
int* func()
{
	int* p = new int(10);
	return p;
}
void test1()
{
	int* p = func();
	cout << *p << endl;
	cout << *p << endl;

	delete p;
	cout << *p << endl;
}

Direct prompt illegal

2. How to use new to open up and release the space of an array in the heap?

void test2()
{
	int* arr = new int[10];
	for (int i = 0; i < 10; i++)
	{
		arr[i] = i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << arr[i] << endl;
	}
}

The results show ten data from 0 to 9.

How to release it? Add delete []arr at the end; When releasing an array, you must add an array identifier.

void test2()
{
	int* arr = new int[10];
	for (int i = 0; i < 10; i++)
	{
		arr[i] = i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << arr[i] << endl;
	}
	delete[] arr;

}

Keywords: C C++

Added by ocpaul20 on Tue, 01 Feb 2022 13:29:22 +0200