【问题标题】:using pointers as private in class to access an array in c++在类中使用指针作为私有指针来访问 C++ 中的数组
【发布时间】:2023-04-06 09:36:01
【问题描述】:

我使用 int 指针作为私有指针来访问数组。当我为存储和获取值的数组编写单独的函数时,程序崩溃。但是,如果我在构造函数中编写获取值和存储值代码,则程序可以正常工作。我无法找到问题所在。

程序 1:(不起作用)

#include<iostream>

using namespace std;

class NewArray{
private:
    int Size = 0;
    int *arrAddr = NULL;

public:
    NewArray(int);
    void SetValue(int);
    void GetValueOf(int);
};

//Array is created
NewArray::NewArray(int arSz){
    int arr[arSz];
    Size = arSz;
    arrAddr = arr;
    cout << "An array of Size " << Size << " is created" << endl;
}

// Store Value function
void NewArray::SetValue(int index)
{
    cin >> *(arrAddr+(index));
}

//Get value function
void NewArray::GetValueOf(int idx)
{
    if ((idx >= Size) || (idx < 0))
    {
       cout << "index value is out of bound" << endl;
    }
    else
    {
       cout << *(arrAddr+idx) << endl;
    }
}

int main()
{
    int arrSize, arrIdx;

    cout << "enter the size of array" << endl;
    cin >> arrSize;

    if (arrSize > 0)
    {
        NewArray ar(arrSize);

        cout << "enter " << arrSize << " values. Enter the values one after the other." << endl;
        for (int i = 0; i < arrSize; i++)
        {
            ar.SetValue(i);
            ar.GetValueOf(i);
        }

        cout << "enter the index to fetch the value" << endl;
        cin >> arrIdx;
        ar.GetValueOf(arrIdx);
    }
    else{
        cout << "invalid input" << endl;
    }
    return 0;
}

程序 2:(正在运行的代码)

#include<iostream>

using namespace std;
// size is passed

class NewArray{
private:
    int Size;
    int *arrAddr;

public:
    NewArray(int);
    void GetValueOf(int);
};

NewArray::NewArray(int arSz){
    int arr[arSz];
    int idx;
    Size = arSz;
    arrAddr = arr;
    cout << "An array of Size " << Size << " is created" << endl;

// Storing values in array
    cout << "enter " << Size << " values. Enter the values one after the other." << endl;
    for (int i = 0; i < Size; i++)
    {
        cin >> *(arrAddr+i);
    }

// To get the value from the index
    cout << "enter the index to fetch the value" << endl;
    cin >> idx;

    if ((idx >= Size) || (idx < 0))
    {
       cout << "index value is out of bound" << endl;
    }
    else
    {
       cout << "The value is " << *(arrAddr+idx) << endl;
    }
}


int main()
{
    int arrSize, arrIdx;

    cout << "enter the size of array" << endl;
    cin >> arrSize;

    if (arrSize > 0)
    {
        NewArray ar(arrSize);
    }

    else{
        cout << "invalid input" << endl;
    }

    return 0;
}

我已经尝试过这个特定示例,当数组大小为 10 并且我尝试写入第 7 个索引时,程序 1 崩溃。

谁能帮我找出原因?

【问题讨论】:

  • 看看我编辑的答案...

标签: c++ arrays pointers constructor


【解决方案1】:

在构造函数NewArray::NewArray() 中创建一个数组,该数组存储在堆栈中。离开构造函数后,它的生命周期结束,从堆栈中移除,因此通过指针arrAddr 访问它是未定义行为。

要简单地解决问题,您需要使用newdelete 在堆上分配数组或将其存储为类成员。

这只是两种实现方式。我推荐任何东西,它们只是可能性

【讨论】:

  • 虽然您的回答是正确的,但我不喜欢您的建议。 Exxept 对于非常罕见的用例,没有理由使用 new 和 delete 在堆上创建一个 c 样式的数组。
  • 谢谢你.. 我认为当使用构造函数创建数组时,它的生命周期将是直到对象被删除。现在我的程序为什么会崩溃是有道理的。
【解决方案2】:

int arr[arSz];

在堆栈上分配,因此它的生命周期仅限于定义它的函数 - 一旦堆栈展开,分配的堆栈内存可供其他人使用。您需要使用 new 运算符在堆上分配内存,以便在函数调用后内存保持持久性。

arrAddr = new int[arSz];

以上在堆上分配内存并且在通过调用delete [] arrAddr 显式删除之前一直可用,在大多数情况下,这应该只在析构函数中完成。

【讨论】:

  • 谢谢你。这有助于我理解某些事情。
【解决方案3】:

忘记了int *。请改用vector。看here

#include <iostream>
#include <conio.h>
#include <vector>

using namespace std;

class Class1 {
private:
    vector<int> intArr;
public:
    void Set(int iValue) {
        intArr.push_back(iValue);
    }
    int Get(int iIndex) {
        return intArr[iIndex];
    }
};

int main() {
    Class1 class1;
    for (size_t i = 0; i < 10; i++)
    {
        class1.Set(i);
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << class1.Get(i) << endl;
    }
    _getch();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 2017-11-06
    • 2014-03-23
    • 1970-01-01
    • 2018-01-04
    • 2011-06-04
    相关资源
    最近更新 更多