[C++] Memory 4 areas---code area, global area, stack area, heap area

every blog every motto: Light tomorrow with today.

0. Preface

Simply record the four areas with c++

1. Text

Function: Data stored in different areas gives us different life cycles, giving us greater flexibility in programming.

1.1 Before running the program

After the program is compiled, an exe executable file is generated, which is divided into two areas before the program is executed

1.1.1 Code Area

Store the binary code of the function body , managed by the operating system

  • Store the machine instructions executed by the CPU
  • The code area is shared, and the purpose of sharing is for frequently executed programs, only a copy of the code is needed in memory
  • The code area is read-only, the reason for making it read-only is to prevent accidental modification of its instructions

1.1.2 Global Area

Store global variables and static variables and constants

  • Global variables and static variables are stored here
  • The global area also includes the constant area , string constants and other constants (const-modified global constants) are also stored here
  • The data in this area is released by the operating system after the program ends
    Insert picture description here

summary:

  • The program in C++ is divided into global area and code area before running
  • Code area features are shared and read-only
  • The global area stores global variables, static variables, and constants
  • The constant area stores const-modified global constants and string constants

1.2 After the program runs

1.2.1 Stack Area

It is automatically allocated and released by the compiler, storing function parameter values , local variables, etc.
Note: Do not return the address of the local variable, the data in the stack area is automatically released by the compiler

#include<iostream>
using namespace std;

int * func(int b) // 形参数据也会存放在栈区
{
    
    
	b = 100;
	int a = 10; // 局部变量存放在栈区,栈区数据在函数执行完成自动释放
	return &a; // 返回局部变量的地址
}


int main()
{
    
    
	// 接受func函数的返回值
	int *p = func(1);

	cout << *p << endl; // 第一次打印正确的数字,是因为编译器做了保留
	cout << *p << endl; // 第二次这个数据就不再保留了
	cout << *p << endl;
	cout << *p << endl;
	system("pause");
}

1.2.2 Stack area

Allocated by the programmer, if the programmer does not release it, the operating system will reclaim it when the program ends.
In C++, new is mainly used to open up memory in the heap area.
Insert picture description here

#include<iostream>
using namespace std;

int * func()
{
    
    
	// 利用new关键字,可以将数据开辟到堆区

	 // 指针 本质是局部变量,放在栈上,指针保存的数据,存放在堆区
	int *p = new int(10); //

	return p;
}

int main()
{
    
    	
	int *point = func();
	cout << "函数返回值为:" << *point << endl;
	system("pause");
}

summary:

  • The heap area data is opened and released by the programmer
  • The heap area data uses the new keyword to open up memory

1.3 new operator (attached)

  • Use the new operator to open up data in the heap area in C++
  • The data created in the heap area is manually created and released by the programmer, using the use operator delete
  • Syntax: new data type
  • The data created with new will return the type pointer corresponding to the data
#include<iostream>
using namespace std;

// 1. new的基本语法
int *func()
{
    
    
	// 在堆区创建整型的数据
	// new返回的是该数据类型的指针
	int *p = new int(10);
	return p;

}

// 2. 在堆区利用new开辟数组
void test02()
{
    
    
	// 创建10整型的数据,在堆区
	int *arr = new int[10]; // 10代表数组个数有10个

	for (int i = 0; i < 10; i++)
	{
    
    
		arr[i] = i + 100; 
	}

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

	// 释放堆区的数组,释放数组,要加 [ ] 才可以
	delete[] arr;
}
void test01()
{
    
    
	int *p = func();
	cout << *p << endl;
	cout << *p << endl;
	// 堆区的数据,由程序员管理开辟,程序员管理释放
	// 如果想释放堆区的数据,利用关键字delete
	delete p;
	//cout << *p << endl; // 内存已经被释放,
}


int main()
{
    
    
	//test01();
	test02();

	system("pause");
}

Guess you like

Origin blog.csdn.net/weixin_39190382/article/details/108049471