Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么可能导致程序崩溃?_C++_Function_Pointers - Fatal编程技术网

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

C++ 什么可能导致程序崩溃?,c++,function,pointers,C++,Function,Pointers,我一直在开发一个创建有序链表的小程序。显然,我的程序中有一个问题(可能),因为每当我执行这个函数时,程序就会崩溃,返回255 基本上,代码的布局如下所示: struct listingRec{ int mlsNum; double price; listingNode* next }; //the calling function void AddListings(listingRec*& topOfList) { int tempmls; char ch;

我一直在开发一个创建有序链表的小程序。显然,我的程序中有一个问题(可能),因为每当我执行这个函数时,程序就会崩溃,返回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');

}
struct listingRec{
国际mlsNum;
双倍价格;
listingNode*下一步
};
//调用函数
作废添加列表(listingRec*&topOfList)
{
int tempmls;
char ch;
做{
tempmls=VerifyMLS();
if(有效的(拓扑列表、临时列表)){
//(被调用函数)
插入列表(topOfList、tempmls);
}
否则{
就我的两分钱:

在崩溃功能中,您有:

while((theMLS > current->mlsNum) && (current != NULL))
如果有可能
current==NULL
然后该行将崩溃,因为它首先计算
theMLS>current->mlsNum
。 因此,如果切换如下语句:

while((current != NULL) && (theMLS > current->mlsNum))
它应该可以工作,因为当第一条语句为false时,第二条语句不会执行

此外,我注意到,你创建了一个新的对象,但你永远不会删除它。这不应该崩溃,但会导致内存泄漏

newNode = new (nothrow) listingRec;

最好的方法是学习如何使用调试器。如果您要编写这样的代码,这将非常有用。检查内存分配。看起来您分配了256个块,并且所有块都完成了。我想您可以从以下表达式的顺序反转开始:
while((theMLS>current->mlsNum)&(current!=NULL))
。也就是说,最好在评估
当前->mlsNum
之前检查空值;而不是在评估之后。WhozCraig,谢谢,你是对的。我只是很沮丧,我自己找不到那个错误。但我确实学到了教训。我觉得自己像个傻瓜。你是对的,我在这个问题上被困了3个小时……谁会想到这是一个如此简单的日志ic错误(我无法捕捉,呃)…哦,我没有删除对象,因为它们是链接列表的一部分,如果需要删除它们,我会确保删除它们并将指针设置为null,没有问题
newNode = new (nothrow) listingRec;