gpt4 book ai didi

c++ - 从堆分配时检测到严重错误

转载 作者:行者123 更新时间:2023-11-30 02:35:37 25 4
gpt4 key购买 nike

<分区>

在我的程序中,我需要创建一个链表,但链表的大小(节点数)是在编译时定义的。 “newcustomer.numT”变量是将要创建的节点数。 为此,我认为我应该有一个指针数组,然后我会通过每个指针从堆中请求内存。但是,不幸的是我也不知道数组的大小。我也是动态创建的。 “transactionn”是一个在单个节点中保存值的结构。

 transactionn **asd;
asd = new transactionn*[newcustomer.numT]; //I created array of pointers
for (int k = 0; k<newcustomer.numT; k++){
asd[k] = new transactionn; //Allocating a node for each pointer in the array
} //End of the "node creating"

newcustomer.trahead = asd[0]; //Assign head to the first node

//Connecting nodes in the linked list to eachother
for (int z = 0; z < (newcustomer.numT)-1; z++){
asd[z]->next = asd[z + 1];
}
asd[newcustomer.numT]->next = NULL; //Set the next of the last node to NULL

当我编译它时,错误列表中没有错误,但在输出中我得到了这个:

Critical error detected c0000374
DataHomework2.exe has triggered a breakpoint.

它触发了这一行的断点:

asd[k] = new transactionn;

我做错了什么?

编辑:我更正了最后一个索引:

asd[(newcustomer.numT)-1]->next = NULL;

当我将这些代码编译成一个完整的程序时,没有错误,程序也没有崩溃。当我将此功能实现到我的主项目程序时再次崩溃。严重错误。

Edit2:这些行是正确的。错误的来源是由于他们从其他函数获得的值。

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