Cheesberry 2022-09-04 18:15 采纳率: 100%
浏览 25
已结题

函数内new分配内存报错

今天写一个顺序表的时候分配内存报错,尝试了好久不知道为什么
代码:

class List{
public:
int* data;
int lenth;

void InitList(List* &L);
void Destory(List* &L);

};
void List::InitList(List* &L){
L->data = new int[100];
L->lenth = 0;
}

void List::Destory(List* &L){
if(!L)
delete[] L->data;
}

int main()
{
List* L1;
L1->InitList(L1);
L1->Destory(L1);
return 0;
}

这一行报错,L->data = new int[100];

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2022-09-04 22:07
    关注

    什么错误提示?供参考:

    #include <stdio.h>
    class List {
    public:
        int* data;
        int lenth;
    
        void InitList(List*& L);
        void Destory(List*& L);
    };
    void List::InitList(List*& L) {
        L->data = new int[100];
        L->lenth = 0;
    }
    void List::Destory(List*& L) {
        if (!L)
            delete[] L->data;
    }
    int main()
    {
        List* L1;
        L1 = new List; //给 L1 new 个对象
        L1->InitList(L1);
        L1->Destory(L1);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月13日
  • 已采纳回答 9月5日
  • 创建了问题 9月4日

悬赏问题

  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥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版本安装软件问题