现在的位置: 首页 > 综合 > 正文

堆上的溢出

2012年08月26日 ⁄ 综合 ⁄ 共 331字 ⁄ 字号 评论关闭

在一次写代码的过程中,突然遇到的一个错误:

如果使用new时不对new操作是否成立的话会导致内存操作错误。下列代码所示:

#include <iostream>
using namespace std;

struct node{
	int value;
	struct node * next;
};

void main()
{
	struct node *head = new node;
	head -> next = NULL;
	int value = 1;
	do{ 
	
		node *temp = new node;
		if(temp){//如果这里不进行判定的话,那么很容易发生内存运行时错误的
				temp -> value = value++;
				temp -> next = NULL;
				head -> next = temp;
				head = temp;
		}
	
	}while (true);

}

抱歉!评论已关闭.