Calcifa97 2019-12-13 16:11 采纳率: 100%
浏览 286
已采纳

请问为什么用new建立动态二维数组就会报错,用malloc就不会报错。

请问为什么用注释掉的部分建立动态二维数组就会报错,用malloc就不会报错?

void Matrix()
{
    int i;
    //double** P_ca_x = new double* [Times];
    //double** P_ca_y = new double* [Times];
    //double** P_ca_z = new double* [Times];
    //double** v_ca_x = new double* [Times];
    //double** v_ca_y = new double* [Times];
    //double** v_ca_z = new double* [Times];
    //double** v_ca = new double* [Times];
    P_ca_x = (double**)malloc(sizeof(double*) * Times);
    P_ca_y = (double**)malloc(sizeof(double*) * Times);
    P_ca_z = (double**)malloc(sizeof(double*) * Times);
    v_ca_x = (double**)malloc(sizeof(double*) * Times);
    v_ca_y = (double**)malloc(sizeof(double*) * Times);
    v_ca_z = (double**)malloc(sizeof(double*) * Times);
    v_ca = (double**)malloc(sizeof(double*) * Times);
    for (i = 0; i < Times; i++)
    {
        //P_ca_x[i] = new double[Num];
        //P_ca_y[i] = new double[Num];
        //P_ca_z[i] = new double[Num];
        //v_ca_x[i] = new double[Num];
        //v_ca_y[i] = new double[Num];
        //v_ca_z[i] = new double[Num];
        //v_ca[i] = new double[Num];
        P_ca_x[i] = (double*)malloc(sizeof(double) * Num);
        P_ca_y[i] = (double*)malloc(sizeof(double) * Num);
        P_ca_z[i] = (double*)malloc(sizeof(double) * Num);
        v_ca_x[i] = (double*)malloc(sizeof(double) * Num);
        v_ca_y[i] = (double*)malloc(sizeof(double) * Num);
        v_ca_z[i] = (double*)malloc(sizeof(double) * Num);
        v_ca[i] = (double*)malloc(sizeof(double) * Num);
    }
}
void Delete()
{
    int i;
    for (i = 0; i < Times; i++)
    {
        //delete[] P_ca_x[i];
        //delete[] P_ca_y[i];
        //delete[] P_ca_z[i];
        //delete[] v_ca_x[i];
        //delete[] v_ca_y[i];
        //delete[] v_ca_z[i];
        //delete[] v_ca[i];
        free(P_ca_x[i]);
        free(P_ca_y[i]);
        free(P_ca_z[i]);
        free(v_ca_x[i]);
        free(v_ca_y[i]);
        free(v_ca_z[i]);
        free(v_ca[i]);
    }
    //delete[] P_ca_x;
    //delete[] P_ca_y;
    //delete[] P_ca_z;
    //delete[] v_ca_x;
    //delete[] v_ca_y;
    //delete[] v_ca_z;
    //delete[] v_ca;
    free(P_ca_x);
    free(P_ca_y);
    free(P_ca_z);
    free(v_ca_x);
    free(v_ca_y);
    free(v_ca_z);
    free(v_ca);
}

在对数组赋值时报错,用malloc则不会报错
图片说明

  • 写回答

2条回答 默认 最新

  • J4cks0n 2019-12-13 16:40
    关注

    不会报错把,麻烦把错误信息贴一下。不应该报错的

    你先把下面的程序编译一下,如果没报错就不是new的问题

    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    int main()
    {
        //申请
        double ** a = new double*[5];
        for(int i = 0; i<5; ++i)
        {
            a[i] = new double[6];
            memset(a[i], 0, sizeof(double)*6);
        }
    
        //赋值
        for(int i = 0; i<5; ++i)
            for(int j = 0; j < 6; ++j)
                a[i][j] = i*j*1.3;
    
        //输出
        for(int i = 0; i<5; ++i)
        {
            for(int j = 0; j < 6; ++j)
                cout<<a[i][j]<<" ";
            cout<<endl;
        }
    
        //删除
        for(int i = 0; i<5; ++i)
        {
            delete[] a[i]; 
        }
        delete[] a;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用