You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用L.deleteEle()后执行L.insert()触发SIGSEGV段错误求助

Segmentation Fault After Calling deleteEle() Then insert() in Custom C++ List Class

Let's break down exactly what's causing your segfault and how to fix it, step by step.

Root Cause: Using malloc() for C++ Objects with Non-POD Members

Your node struct contains a std::string member, which is a non-POD (Plain Old Data) C++ object. When you use malloc() to allocate memory for a node:

nPtr = (nodePtr)malloc(sizeof(node));

malloc() only allocates raw memory—it does not call the constructor for std::string. This leaves the data member in an uninitialized, invalid state. When you try to assign to it later with nPtr->data = element;, the std::string's assignment operator tries to operate on garbage internal state (like a pointer to random memory), which triggers the segmentation fault.

Why did this work before calling deleteEle()? That's just the nature of undefined behavior—sometimes bad code appears to work by accident. The deleteEle() operation likely altered memory layout or triggered some implicit check that made the hidden error surface.

Fixes to Resolve the Issue

  1. Replace malloc()/free() with new/delete
    C++ requires using new to create objects (it allocates memory and runs constructors) and delete to destroy them (runs destructors and frees memory).

    • In insert(), replace the malloc line with:
      nPtr = new node;
      
    • In every spot in deleteEle() where you call free(cPtr), replace it with:
      delete cPtr;
      

    This ensures your std::string members are properly constructed and destructed, eliminating the invalid state that caused the segfault.

  2. Fix Edge Case in deleteEle() When Removing the Last Element
    When you delete the final node, your current code sets tail = pPtr, but if count drops to 0, pPtr will be NULL (since you were at the last element), and you still need to set head to NULL to avoid invalid pointers later. Update that branch:

    if(position==count){
        tail = pPtr;
        delete cPtr;
        count -= 1;
        if (count == 0) {
            head = NULL;
            tail = NULL;
        } else {
            tail->next = NULL;
        }
        return true;
    }
    
  3. Add the Missing size() Member Function
    Your main() calls L.size(), but your List class doesn't have this method—this would cause a compile error before even hitting the segfault. Add this to your class public section:

    int size() const {
        return count;
    }
    

Why These Changes Work

  • new and delete handle the lifecycle of std::string correctly, ensuring no uninitialized object states.
  • Fixing the last-element deletion edge case prevents dangling pointers to deallocated memory.
  • Adding size() resolves the compile error so your code can run as intended.

内容的提问来源于stack exchange,提问作者1337r0b07

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:40:20