gpt4 book ai didi

c++ - 写入动态二维数组时访问冲突...有时

转载 作者:太空宇宙 更新时间:2023-11-04 15:28:26 25 4
gpt4 key购买 nike

该程序旨在生成一个动态数组,但是在给定特定维度时写入时会出现访问冲突错误。例如:R = 6, C = 5 崩溃,但是 R = 5, C = 6 不会。如果您想知道,“修复”这个损坏的程序不是我的作业,这是我们在类里面教授的方法。我的评估的一部分也是使用这种方法,所以 vector 出来了。提前致谢!

#include <iostream>

using namespace std;

int main(){

const int R = 6;
const int C = 5;

char **d;

d = new char *[R];

for(int i=0; i<C; ++i){
d[i] = new char[C];
}

//initialise
for(int i=0; i<R; ++i){
for(int j=0; j<C; ++j){
d[i][j] = 'd';
cout<<d[i][j];
}
cout<<endl;
}
cout<<endl;

system("pause");
return 0;
}

最佳答案

for(int i=0; i<C; ++i){
d[i] = new char[C];
}

应该是

for(int i=0; i<R; ++i){
d[i] = new char[C];
}

关于c++ - 写入动态二维数组时访问冲突...有时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2619480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com