【问题标题】:C++ Error : Unhandled exception at 0x00934ABB (linked list, addressbook)C++ 错误:0x00934ABB 处未处理的异常(链表、地址簿)
【发布时间】:2014-03-07 09:10:14
【问题描述】:

我是 C++ 新手,我有一个关于创建此通讯簿程序的项目,但我在使用下面的代码时遇到了问题。该代码应该创建记录。

cin >> ch;
switch(ch)
{
    case '1':
        system("cls");
        cout << "\n\nINSERT RECORD";
                        cout << "\n\nInput FIRST NAME: ";
                        cin.ignore();
                        getline(cin,firstname,'\n');
                        cout << "\n\nInput LAST NAME: ";
                        getline(cin,lastname,'\n');
                        cout << "\n\nInput PHONE NUMBER: ";
                        getline(cin,phonenumber,'\n');
                        cout << "\n\nInput DAY OF BIRTH: ";
                        getline(cin,dayofbirth,'\n');
                        cout << "\n\nInput MONTH OF BIRTH: ";
                        getline(cin,monthofbirth,'\n');
                        cout << "\n\nInput YEAR OF BIRTH: ";
                        getline(cin,yearofbirth,'\n');
                        cout << "\n\nInput AGE: ";
                        getline(cin,age,'\n');
                        cout << "\n\nInput STREET NAME (Address): ";
                        getline(cin,streetname,'\n');
                        cout << "\n\nInput CITY (Address): ";
                        getline(cin,city,'\n');
                        cout << "\n\nInput STATE (Address): ";
                        getline(cin,state,'\n');
                        cout << "\n\nInput ZIP CODE (Address): ";
                        getline(cin,zipcode,'\n');
                        cout << "\nRecord inserted!";
                    current = ad.AddNode(temp);
            ad.userPromptStatement();
    break;


    node* AddressBook::AddNode(nodePtr temp)
    { 
        string firstname;
        string lastname;
        string phonenumber;
        string dayofbirth;
        string monthofbirth;
        string yearofbirth;
        string age;
        string streetname;
        string city;
        string state;
        string zipcode;
        AddressBook ad;

           if(head != NULL)
            {
                current = head;
                while(current -> next != NULL)
                {
                    current = current -> next;
                }
                current = new node;
                current -> next -> firstname = temp -> firstname;
                current -> next -> lastname = temp -> lastname;
                current -> next -> phonenumber = temp -> phonenumber;
                current -> next -> dayofbirth = temp -> dayofbirth;
                current -> next -> monthofbirth = temp -> monthofbirth;
                current -> next -> yearofbirth = temp -> yearofbirth;
                current -> next -> age = temp -> age;
                current -> next -> streetname = temp -> streetname;
                current -> next -> city = temp -> city;
                current -> next -> state = temp -> state;
                current -> next -> zipcode = temp -> zipcode;
                current -> next = nullptr;
                return current;
                ad.userPromptStatement();
            }
           else
            {
                head = new node;
                head -> firstname = temp -> firstname;
                head -> lastname = temp -> lastname;
                head -> phonenumber = temp -> phonenumber;
                head -> dayofbirth = temp -> dayofbirth;
                head -> monthofbirth = temp -> monthofbirth;
                head -> yearofbirth = temp -> yearofbirth;
                head -> age = temp -> age;
                head -> streetname = temp -> streetname;
                head -> city = temp -> city;
                head -> state = temp -> state;
                head -> zipcode = temp -> zipcode;
                head -> next = nullptr;
                return current;
            }
    }

我在“xstring”中不断收到错误。

当我创建 1 条记录时,错误不会发生,但是当我创建第二条记录时,我得到这个: “dummy3.exe 中 0x00934ABB 处未处理的异常:0xC0000005:访问冲突读取位置 0xCDCCDCDE5。”

bool _Grow(size_type _Newsize,
    bool _Trim = false)
    {   // ensure buffer is big enough, trim to size if _Trim is true
    if (max_size() < _Newsize)
        _Xlen();    // result too long
    if (this->_Myres < _Newsize) /*** <- next statement that will be executed points to this line ***/
        _Copy(_Newsize, this->_Mysize); // reallocate to grow
    else if (_Trim && _Newsize < this->_BUF_SIZE)
        _Tidy(true, // copy and deallocate if trimming to small string
            _Newsize < this->_Mysize ? _Newsize : this->_Mysize);
    else if (_Newsize == 0)
        _Eos(0);    // new size is zero, just null terminate
    return (0 < _Newsize);  // return true only if more work to do
    }

有什么想法吗?我对“xstring”部分一无所知..

【问题讨论】:

  • 您正在访问一个空指针。可能有帮助:ericlippert.com/2014/03/05/how-to-debug-small-programs/
  • 您的 AddNode (a) 检测到 NULL 头,分配一个节点,泄漏其内存,然后返回一个神秘指针 current,该指针从未在函数范围内设置或声明,或者(b) 检测到非 NULL 头,走到链表的末尾,然后立即丢失刚刚获得的用于分配新节点的指针,错误地(重复地)取消引用其 next,返回指向该新节点的指针但从不将其添加到列表中。即这段代码还没有开始战斗。
  • temp 是否已经在其他地方动态分配,而您要做的就是将其链接到 head 指向的(可能为空的)链表的尾部? current 来自哪里?这显然是一个函数,但 currenthead 似乎都没有被声明或传入。发布 real 函数,带参数和 记录任何全局/外部变量。这段代码中有很多错误,修复它们取决于这些信息。
  • 不要从下划线开始你的变量/函数/任何名称:stackoverflow.com/questions/228783/…

标签: c++ string linked-list addressbook unhandled-exception


【解决方案1】:

你的问题出在这里:

current = new node;
current -> next -> firstname = temp -> firstname;

您刚刚创建了一个新节点(替换当前节点),然后您尝试访问其未初始化的子节点(下一个)。

这样应该会更好:

current -> next = new node;
current -> next -> firstname = temp -> firstname;
...
current -> next -> next = nullptr;
return current -> next;

另一个提示:如果 tmp 是动态创建的,那么考虑直接使用它而不是复制它的元素。像这样:

current -> next = tmp;

我不知道你所有的代码,所以我不能说这是否适合你。

【讨论】:

  • 我很确定应该删除current = new node;,其余部分保持原样。再看一遍。 (不能承受不适当的返回值)。
  • 哦,是的。现在我得到了错误。我会更新我的答案。
  • 是的,both 返回值没有意义,也不清楚传入的temp 本身是否是动态分配的,应该只是链接为current-&gt;next。 (并且您拥有的代码现在分配了一个新节点,然后覆盖了current 节点的现有数据的内容,因此仍然不正确)。
  • 我将它添加到我的答案中,但是否使用它是他的选择(也许他有充分的理由复制它)。
【解决方案2】:

这个错误的原因可能是试图访问空指针。

您应该在使用前创建 temp。我看不到任何名称为 temp 的指针。请先创建 temp,然后使用 malloc 或 memset 将其分配到内存中。然后就可以传递错误了。

【讨论】:

  • 这是C++,不要使用“邪恶”的malloc,使用newdelete
  • 是的,new 和 delete 更好 malloc 是 C thx 中的另一个选项,供您参考。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 2013-03-05
  • 1970-01-01
  • 2016-04-11
  • 2017-10-11
  • 1970-01-01
相关资源
最近更新 更多