C++ 中动态二维数组的访问冲突
access violation with dynamic 2-dimension array in C++
感谢您在这里提供帮助
我想将动态二维尺寸作为字符
int n;
char** stars;
int main() {
cin >> n;
for (int i = 0;i < n;i++) {
stars = new char* [n];
stars[i] = new char[n];
for (int j = 0;j < n;j++) {
stars[i][j] = '*';
}
}
for (int i = 0;i < n;i++) {
for (int j = 0;j < n;j++)
cout << stars[i][j]; //this is where access violation is occured
cout << endl;
}
return 0;
}
而且我还想知道,如果我在这个数组上放 *,那么每次插入时都会在内存(可能是此代码中的堆栈(上生成包含"*"的新 char 数据?
stars = new char* [n];
错误地定位在循环中,因此您为每个字符串重新分配它,并且在新分配的矩阵中仅分配一个字符串。
stars = new char* [n];
为什么要在 for 循环中声明这一点? 它将为每个循环一次又一次地分配。 只需在外面声明即可
相关文章:
- 将值从二维数组输出到文本文件
- 在二维数组中查找最小值和最大值?
- 移动二维数组中的字符
- 如何正确填充在堆上分配的二维数组?
- 传递二维数组时出现问题
- 具有随机数的二维数组不会更改
- 如何在C++中获取二维数组中最少的一列数?
- 如何使用用户输入变量制作二维数组?
- C++ 中动态二维数组的访问冲突
- 遍历二维数组的所有子数组
- 将二维数组的所有元素插入到一维数组中
- 在函数中传递二维数组
- 通过访问二维数组实现双线性插值的概念类似于一维数组
- 封装的二维数组与普通版本相比——访问速度
- 是否可以使用负索引访问二维数组
- 从行指针访问二维数组的行索引
- 访问传递给函数的二维数组时出现分段错误
- 如何正确访问指向二维数组的指针的元素
- 删除动态创建的二维数组时出现访问冲突消息
- 访问非托管矢量(C++)中的二维数组