gpt4 book ai didi

c++ - 什么可能导致程序崩溃?

转载 作者:行者123 更新时间:2023-11-28 02:43:06 28 4
gpt4 key购买 nike

<分区>

我一直在研究一个创建有序链表的小程序。显然,我的程序有问题(也许),因为每当我执行此函数时,程序都会崩溃并返回 255。

基本上,代码是这样布局的:

struct listingRec{
int mlsNum;
double price;
listingNode* next
};

//the calling function
void AddListings(listingRec*& topOfList)
{

int tempmls;
char ch;

do{

tempmls = VerifyMLS();

if(ValidMLS(topOfList, tempmls)){

//(the called function)
InsertListing(topOfList, tempmls);


}

else{

cout << endl << endl
<< "*****ERROR*****"
<< endl
<< "MLS# " << tempmls << " already exists."
<< endl << endl << endl;

}

cout << endl
<< "Add another listing (Y/N)? : ";
cin >> ch;
ch = toupper(ch);

}while(ch == 'Y');

}

我认为是崩溃函数。

void InsertListing(listingRec*& topOfList, int theMLS)
{

listingRec *current,
*previous,
*newNode;

current = topOfList;
previous = NULL;

while((theMLS > current->mlsNum) && (current != NULL))
{
previous = current;
current = current->next;
}

newNode = new (nothrow) listingRec;

if(newNode == NULL)
{
cout << "Heap error - could not allocate memory" << endl;
}

else
{
if(previous == NULL)
{
newNode->next = current;
topOfList = newNode;
}
else if(current == NULL)
{
previous->next = newNode;
newNode->next = NULL;
}
else
{
previous->next = newNode;
newNode->next = current;

}

newNode->mlsNum = theMLS;
newNode->price = VerifyPrice();

}

}

很抱歉,如果这个问题对你们中的很多人来说有点新手(我敢肯定)。有趣的是,我发誓这个程序昨晚能正常工作,但出于某种原因,它今天决定不工作了,我完全被难住了。怎么可能昨天有用,今天就不行。

即使您一无所知,如果您能给我任何可能导致崩溃的提示或建议。我将不胜感激。谢谢。

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