Getting to understand C ++ stage two core articles (1) - c ++ object-oriented overview, memory analysis, references

1.c ++ memory partition model

c ++ program running in the process, the memory is divided into the following four partitions

Code area: Programs binary code of the program, including comments will be placed in this area

Global Area: storing static variables, global variables, constants (string constants and const modified constant), the data in this area will be freed by the operating system at the end of the program

using namespace std;
//不在任何函数内的变量是全局变量
int a = 10;
int b = 23;

int main() {
	//在某个函数内的变量
	int c = 12;
	cout << (int)&a << endl;
	cout << (int)&b << endl;
	cout << (int)&c << endl;

}

Stack Area: used to store local variables, function parameters, etc., are automatically released and allocated by the compiler, so it is not a return address stack area,

#include <iostream>
#include <string>
using namespace std;

int* f() {
	int a = 10;
	return &a;//栈区不要返回地址。局部变量是由编译器自动释放和分配
}
int main() {
	
	int* p = f();
	cout << *p;//10 第一次编译器会做保留
	cout << *p;//不会打印10
}

Heap: storage objects (using the new operator, will be introduced later) released by the programmer assigned, if we do not release the program ends by the operating system release

c ++ by the new keyword will open up data to the stack area

#include <iostream>
using namespace std;
int* f() {
	/*
	*1.指针本身也是局部变量,存放在栈长中,但是保存的数据在堆中
	2.new关键字开辟一块堆区,返回的是该数据类型的指针
	*/
	int *a = new int(10);
	return a;
}
int main() {
	int* p = f();
	cout << *p;//10
	cout << *p;//10
	//3.堆区的数据程,序员可以通过该delete释放
	delete p;
	//cout << *p;异常
}

 If you are a new array

//new一个数组
int* arr = new int[10];
//释放一个数组
delete[] arr;

note

Code area and the global area are compiled into exe executable file when it is already there, but the stack area and heap area after the implementation of the program exe file generated

2. Why divide memory (memory division sense)

The different data in different areas, giving different life cycles, improve flexibility of programming

3. references

Use a reference to a variable surnamed

#include <iostream>
using namespace std;

int main() {	
	int a = 10;
	/*1.定义引用的格式  数据类型 &别名=原名
	*2.引用必需要初始化
	*3.引用初始化后,不可更改
	*/
	int& b = a;
	cout << b;//10
}

Previous article modifies the transfer address argument, argument passed by value does not modify, as a function of a reference parameter modifies the argument, the process of simplifying complex argument a pointer to modify

#include <iostream>
using namespace std;
void swap(int &a,int &b) {
	int temp = a;
	a = b;
	b = temp;
}
int main() {	
	int a = 10;
	int b = 20;
	swap(a,b);
	cout << a;//20
	cout << b;//10
}

 Local variables can not be returned as the return value of the function

#include <iostream>
using namespace std;
int& f() {
	int a = 10;//栈区中的局部变量在函数执行完后释放
	return a;
}
int main() {	
	int &a = f();
	cout << a;//10 编译器保留
	cout << a;//不在是10
}

If local static variables, you can return

#include <iostream>
using namespace std;
int& f() {
	static int a = 10;//栈区中的局部变量在函数执行完后释放
	return a;
}
int main() {	
	int &a = f();
	cout << a;//10
	cout << a;//10
}
#include <iostream>
using namespace std;
int& f() {
	static int a = 10;//栈区中的局部变量在函数执行完后释放
	return a;
}
int main() {	
	int &a = f();
	cout << a;//10
	cout << a;//10
	f() = 100;//如果函数的返回是一个引用,可以作为左值
	cout << a;//100
	cout << a;//100
}

Reference is essentially a pointer constant

int main() {	
	int a = 1;
	//内部发现是引用,自动转成指针常量 int * const b=&a;
	int& b = a;
	b = 2;//内部发现是引用,自动转成*b=20;
	cout << a;//2
	cout << b;//2
}

 Const reference

#include <iostream>
using namespace std;
//使用const修改函数形参,防止误操作
void f(const int& a) {
	//a = 100;不允许修改
}
int main() {	
	int& b = 1;//引用本身需要一个合法内存空间,所以这行代码有误
	int a = 10;
	f(a);
}

 

4. Functions Related

The basis of the previous series c ++ knowledge about the functions described here to add some functions advanced knowledge

1.c ++ functions can have default values ​​in

#include <iostream>
using namespace std;
//1.c++中函数可以有默认值,并且某个位置有了默认值,那么从这个位置开始左到右都的有默认值
int f(int a, int b = 10,int c=20) {
	return a + b + c;
}
int main() {	
	int a = 10;
	//2.如果函数有默认值,当我们传值使用传递的值,不传值使用默认的
	cout<<f(a);//40
	cout << f(a, 20);//50
}
//3.声明和实现只能有一个有默认参数
int f1(int a, int b = 10);
int f1(int a, int b) {
	return a + b;
}

2.c ++ function parameters can be used placeholder placeholder, the calling function is necessary to fill this position

#include <iostream>
using namespace std;

//1.只写一个数据类型就是占位
void f(int a,int) {
	cout << "test";
}
//2.占位参数可以有默认值
void f1(int a, int=10) {
	cout << "test";
}
int main() {	
	int a = 10;
	f(a, 10);//占位参必须填补
}

3. function overloading

Definition: under the same scope, two different functions in different type or parameters or a different number of parameters sequentially. At this time, these two functions can be the same name. Improve reusability

#include <iostream>
using namespace std;

void f(int a,int b) {
	cout << "test";
}

void f(int a) {
	cout << "test";
}
int main() {	
	int a = 10;
	f(a, 10);
}

Note: The function returns the value of the condition is not overloaded as a function

4. Reference may also function as a heavy load condition

#include <iostream>
using namespace std;

void f(int &a) {//int &a=10;不合法
	cout << "test";
}

void f(const int &a) {//const int &a=10;合法
	cout << "test111";
}
int main() {	
	int a = 10;
	f(a);//test
	f(10);//test111
}

5. Function overloading encountered default parameters Note

void f(int a,int b=10) {//int &a=10;不合法
	cout << "test";
}

void f(int a) {//const int &a=10;合法
	cout << "test111";
}
int main() {	
	int a = 10;
	f(a);//报错,出现二义性
}


Guess you like

Origin www.cnblogs.com/javayihao/p/11937691.html