C++语法 内存区域 和 引用

代码区

存放函数体的二进制代码 由操作系统进行管理

全局区 

存放全局变量和静态变量以及常量 存放全局变量 静态变量 常量(字符串常量 全局const常量)

栈区

由编译器自动分配释放,存放函数的参数值,局部变量等。不要返回局部变量的地址

堆区

由程序员分配和释放 若程序员不释放程序结束的时候由操作系统回收 cpp 中使用new进行开辟内存

C++语法 内存区域 和 引用_第1张图片

C++语法 内存区域 和 引用_第2张图片

new操作符

c++中利用new操作符在堆区开辟内存 需要程序员手动释放。

#include 
#include "mathutil.hpp"
#include 
using namespace std;
int *func() {
    int *p = new int(10);
    return p;
}
void test1() {
    int *array = new int[10];
    for (int i = 0; i < 10; i++) {
        array[i] = 100 + i;
    }
    for (int i = 0; i < 10; i++) {
        std::cout << array[i] << std::endl;
    }
    // 释放数组需要在变量前➕一个【】
    delete[] array;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    test1();
    int * p = func();
    std::cout << *p << std::endl;
    std::cout << *p << std::endl;
    std::cout << *p << std::endl;
    std::cout << *p << std::endl;
    delete p;
    std::cout << *p << std::endl;
    return 0;
}


Hello, World!
10
10
10
10
-559071216
Program ended with exit code: 0

引用

给变量起别名

语法 数据类型 &别名 = 原名

#include 
#include "mathutil.hpp"
#include 
using namespace std;
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    int a = 10;
    int &b = a;
    std::cout << "a=" << a << "b=" << b << std::endl;
    b = 100;
    std::cout << "a=" << a << "b=" << b << std::endl;
    //引用必须初始化
    //int &c;
    int d = 20;
    b = d; //赋值操作并不是更改引用
    std::cout << "a= " << a << "b="  << b << "d  = " << d  << std::endl;

    return 0;
}

注意

引用必须要初始化

引用初始化后就不可以更改

引用做函数参数

作用 函数传参时可以利用引用的技术让形参修改实参

优点 可以简化指针修改实参

通过引用参数产生的 效果和按地址传递是一样的 引用的语法更清楚简单

//
//  main.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#include 
#include "mathutil.hpp"
#include 
using namespace std;
void swapNum(int a,int b) {
    int temp = a;
    a = b;
    b = temp;
}

void swapNum2(int* a,int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
void swapNum3(int &a,int &b) {
    int temp = a;
    a = b;
    b = temp;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    int a = 10;
    int b = 20;
    swapNum(a, b);
    std::cout << "a=" << a << "b=" << b << std::endl;
    
    swapNum2(&a, &b);
    std::cout << "a=" << a << "b=" << b << std::endl;
    
    swapNum3(a, b);
    std::cout << "a=" << a << "b=" << b << std::endl;

    return 0;
}

引用做函数返回值

不要返回局部变量的引用

Reference to stack memory associated with local variable 'a' returned
#include 
#include "mathutil.hpp"
#include 
using namespace std;
int& test03() {
    int a = 10; /// 局部变量在栈区
    return a;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    int &a =  test03();
    std::cout << "a=" << a << std::endl;

    return 0;
}

 

函数的调用可以作为左值

//
//  main.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#include 
#include "mathutil.hpp"
#include 
using namespace std;
int& test04() {
   static int a = 10; /// 局部变量在栈区
    return a;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    int &a =  test04();
    std::cout << "a=" << a << std::endl;
    std::cout << "a=" << a << std::endl;
    test04() = 1000;
    std::cout << "a=" << a << std::endl;
    std::cout << "a=" << a << std::endl;

    return 0;
}



Hello, World!
a=10
a=10
a=1000
a=1000
Program ended with exit code: 0

引用的本质

引用的本质在cpp中就是一个指针常量C++语法 内存区域 和 引用_第3张图片

常量引用

用来修饰形参防止误操作

//
//  main.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#include 
#include "mathutil.hpp"
#include 
using namespace std;
void printNum(int &num) {
    std::cout  << num << std::endl;
    num = 100;
}
void printNum2(const int &num) {
    std::cout  << num << std::endl;
    //num = 100;
    //Cannot assign to variable 'num' with const-qualified type 'const int &'
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    // 引用必须引用一块合法的内存空间
    //int &a = 20;
    // 加上const 之后 这里相当于 编译器将代码修改为 int temp = 10; const int &ref = temp;
    const int &ref = 10; //  加入const 之后变为只读不可以修改
    // 引用本质是指针常量 指针常量 特点是 不能修改指向 只能修改指针保存地址的值
    
    int num = 22;
    printNum(num);
    std::cout  << num << std::endl;
    int num2 = 20;
    printNum2(num2);
    
    return 0;
}

你可能感兴趣的:(c++)