不确定,为什么此代码,同时扫描动态分配的数组导致分段错误
Not sure, why this code, while scanning the dynamically allocated array causing segmentation fault
尝试读取动态分配的二维数组。但有分割错误。找不到原因。据我了解,它应该有效
扫描阵列时。 在第一次输入时,它会导致分段错误
class DiognalSum
{
public :
int **a;
public :
DiognalSum(int n)
{
int **a = new int*[n];
for(int i = 0; i < n; i++)
{
a[i] = new int[n];
}
}
public :
void getArray(int n)
{
int input;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin >> input;
a[i][j] = input; // segmentation fault is here
}
}
}
void printArray(int n)
{
cout << "Out " << endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout << a[i][j];
}
cout << endl;
}
}
};
int main()
{
DiognalSum d(3);
d.getArray(3);
d.printArray(3);
return 0;
}
DiognalSum(int n)
{
//int **a = new int*[n]; // <-- you have locally declared `int **a`
a = new int*[n]; // <-- correct would be to use DiognalSum::a
for(int i = 0; i < n; i++)
{
a[i] = new int[n];
}
}
相关文章:
- 数组的指针从不分段故障
- 创建结构的数组时遇到分段错误
- 分段错误 - 读取初始化指针的数组
- SIGSEGV, 分段错误. 而 printf() 数组索引的值
- 在函数 strcpy() 中访问字符数组时出现分段错误
- 用于查找数组中最大元素的出现次数的代码,给出分段错误
- 尝试将对象插入空指针数组时出现分段错误
- 分段 使用 MPI_Gather 收集字符数组时出错
- 尝试从动态分配的二维数组 C++ 中读取值时出现分段错误,并在尝试删除它时给出 munmap_chunk():
- C++ .我正在尝试通过使用开关内的数组来获取用户输入,但是当我运行代码时,它显示分段错误?
- 当我在将文件读取到C字符串的2D数组后尝试对任何内容进行定制时,C++分段错误
- 使用结构数组取消引用时的分段错误
- 指针数组的分段错误
- 在编写代码以查找数组中的对数时获取分段错误
- 分段失败反向数组
- 分段冲突 分子数组中值的分段错误 c++
- 在 c++ 中将动态分配的数组作为结构成员的分段错误?
- 字符数组中的分段错误
- 传递动态分配的 2D 字符数组会导致分段错误
- 字符指针的 2D 数组 --> 分段错误?