DDWpro 2021-11-23 10:08 采纳率: 66.7%
浏览 16

C++中动态二维数组赋值运行出现问题

#include<stdio.h>
int main()
{

int m;
int n;
int rows,columns;
int **array2D;
array2D=new int*[m];
for(int i=0;i<m;i++)
{
    array2D[i]=new int[n];
}
printf("Please input the number of rows you want to set as the length of your array\n");
scanf("%d",&rows);
printf("Please input the number of column you want to set as the width of your array\n");
scanf("%d",&columns);
printf("Please input %d numbers to set as the content of your array\n",rows*columns);
for(int m=0;m<=rows-1;m++)
{
    for(int n=0;n<=columns-1;n++)
    {
        scanf("%d",&array2D[m][n]);
    }
}
for(int i=0;i<=m;i++)
{
    delete array2D[i];
}
delete array2D;
return 0;
 

}
运行结果:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc


Process exited after 3.181 seconds with return value 3
请按任意键继续. . .

  • 写回答

1条回答 默认 最新

  • fortunely2 2021-11-23 10:23
    关注

    程序逻辑混乱。
    m,n是局部变量,你初始化了吗?如何初始化的?为什么向操作系统申请一个没有明确大小的数组array2D=new int*[m];?你确定m及后面的n是你想要的值?

    评论

报告相同问题?

问题事件

  • 创建了问题 11月23日

悬赏问题

  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物