-1
你好,我是一名思考链表的初学C++程序员。我想出了下面的代码,但是当我运行它时,我的电脑崩溃了。我想知道是什么导致我的电脑做到这一点。为什么我的电脑在我建立链表时崩溃?
#include <iostream>
using namespace std;
struct list
{
int value;
list* nextlist;
};
list* getnewstruct (list* phead, int nextvalue);
void printarray (list* phead);
int main()
{
int nextvalue = 0;
list* phead = NULL;
while (nextvalue < 5) {
phead = getnewstruct (phead, nextvalue);
}
printarray (phead);
}
list* getnewstruct (list* phead, int nextvalue)
{
list* newlist = new list;
newlist->value = nextvalue;
newlist->nextlist = phead;
return newlist;
}
void printarray (list* phead)
{
while (phead->nextlist != NULL) {
cout<<phead->value<<endl;
printarray (phead->nextlist);
}
}
in main,nextvalue永远不会更新。 – Max 2014-10-02 14:36:08
您需要在具有独立进程的操作系统上解决这个问题,以免它使整个计算机崩溃。 – 2014-10-02 14:37:36
nextvalue始终为0,所以始终为<5,所以它会循环直到内存不足,然后bad_alloc但可能会冻结您的计算机,因为它在过渡期内的内存不足 – CashCow 2014-10-02 14:39:06