c++基础代码备忘录-指针与数组

本文探讨了C++中指针和数组的边界问题,强调越界可能导致的错误,以及内存的正确申请、释放和初始化的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系列文章目录



前言


一、指针

指针地址的越界,编译器难以发现错误

代码如下:

#include <iostream>
using namespace std;

int main()
{
    int a;
    int num = 0;
    int * p = &num;
    // 右移越界
    p[-1] = 2; //out of bound
    p[0] = 3; //okay
    // 左移越界
    *(p+1) = 4; //out of bound
    // 根据指针地址对元素数值进行修改
    cout << "num = " << num << endl;
    
    return 0;
}

2.数组

数组索引的越界,编译器也不易发现错误

代码如下:

#include <iostream>
using namespace std;
int main()
{
    int num_array[5]; 
    // 内存是连续的,数组溢出产生的错误可能很难发现
    // -1是下溢的数组
    for(int idx = -1; idx <= 5; idx++) //out of bounds
        num_array[idx] = idx * idx;

    for(int idx = -1; idx <= 5; idx++) //out of bounds
        cout << "num_array[" << idx << "] = " << num_array[idx] << endl;

    return 0;
}
// num_array[-1] = -1
// num_array[0] = 0
// num_array[1] = 1
// num_array[2] = 4
// num_array[3] = 9
// num_array[4] = 16
// num_array[5] = 25

2.指针内存的申请与释放

代码如下(示例):


#include <iostream>
using namespace std;

struct Student
{
    char name[4];
    int born;
    bool male; 
};

int main()
{
    // c++申请一个堆内存
    //allocate an int, default initializer (do nothing)
    int * p1 = new int; 
    //allocate an int, initialized to 0
    int * p2 =  new int();
    //allocate an int, initialized to 5
    int * p3 =  new int(5); 
    //allocate an int, initialized to 0
    int * p4 =  new int{};//C++11    
    //allocate an int, initialized to 5
    int * p5 =  new int {5};//C++11

    //allocate a Student object, default initializer
    Student * ps1 = new Student;
    //allocate a Student object, initialize the members
    // g++ newdelete.cpp -std=c++11// c++11以及更高版本的标准才能使用{}进行初始化
    Student * ps2 = new Student {"Yu", 2020, 1}; //C++11
    // Student * ps2 = new Student "Yu", 2020, 1; //C++11

    //allocate 16 int, default initializer (do nothing) 
    int * pa1 = new int[16];
    //allocate 16 int, zero initialized 
    int * pa2 = new int[16]();
    //allocate 16 int, zero initialized 
    int * pa3 = new int[16]{}; //C++11
    //allocate 16 int, the first 3 element are initialized to 1,2,3, the rest 0
    int * pa4 = new int[16]{1,2,3}; //C++11

    //allocate memory for 16 Student objects, default initializer
    Student * psa1 = new Student[16];
    //allocate memory for 16 Student objects, the first two are explicitly initialized
    Student * psa2 = new Student[16]{{"Li", 2000,1}, {"Yu", 2001,1}}; //C++11
    cout << psa2[1].name << endl;
    cout << psa2[1].born << endl;

    //deallocate memory
    delete p1;
    //deallocate memory
    delete ps1;

    //deallocate the memory of the array
    delete pa1;
    //deallocate the memory of the array
    delete []pa2;

    //deallocate the memory of the array, and call the destructor of the first element
    delete psa1;
    //deallocate the memory of the array, and call the destructors of all the elements
    delete []psa2;

    return 0;
}

总结

进行数组操作时要主要检查的数组大小,
指针申请堆内存后一定要记得释放内存,
不要轻易对已有指针赋值,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云朵不吃雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值