【问题标题】:Segmentation fault without "const char*"没有“const char *”的分段错误
【发布时间】:2012-01-27 06:14:30
【问题描述】:

我有一些问题。 我尝试此代码并收到“分段错误”错误:

#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

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

void push(Stack* top, int value) {
    Stack *ntop = new Stack;
    ntop->value = top->value;
    ntop->next = top->next;
    top->next = ntop;
    top->value = value;
}

int pop(Stack* top) {
    int val = top->value;
    top->value = top->next->value;
    top->next = top->next->next;
    return val;
}

int main()
{
    Stack *top;
    top->next = NULL;
    push(top, 20);
    cout << pop(top);
}
[10:40:46] [~] >> g++ 3.cpp -o 3 && ./3
分段错误

但如果我在 Stack *top; 之前添加 const char* test = ""; 它可以正常工作:

int main()
{
    const char* test = "";
    Stack *top;
    top->next = NULL;
    push(top, 20);
    cout << pop(top);
}
[10:47:33] [~] >> g++ 3.cpp -o 3 && ./3
20

我的错在哪里?

【问题讨论】:

  • 您已经标记了这个c,但写了c++ 标题和using namespace std;——这是哪个?你想写 C 还是 C++?最好选择一个并坚持下去——两者的互换性比以前少了。
  • 这只是我的大学练习,我更喜欢 STL

标签: c++ pointers stack segmentation-fault


【解决方案1】:

问题出在这里:

Stack *top;
top->next = NULL;

您正在引用一个未初始化的指针。那是未定义的行为。所以任何事情都可能发生,它可能与周围的代码不一致。

我想你忘了为top实际分配一些东西。

int main()
{
    Stack *top = new Stack;  //  Allocate

    top->next = NULL;
    push(top, 20);
    cout << pop(top);

    delete top;   //  Free

    return 0;
}

*虽然我想指出,代码中仍然存在内存泄漏。

【讨论】:

    【解决方案2】:
    int main()
    {
        Stack *top;
        top->next = NULL;
    

    如果这是原始 C,您将在垃圾位置写入 NULL —— top 变量尚未初始化,因此它指向垃圾。 -&gt;next 将跟随您的垃圾指针,然后以 4 或 8 个字节的偏移量写入它。还是垃圾。

    也许 C++ 为你做了一些神奇的struct == class 神奇的初始化——我不太懂 C++ 无法评论——但你可能还在看垃圾。

    添加test = "" 足以改变内存布局,以便您覆盖进程地址空间内的某些内容。它仍然是垃圾,所以谁知道你打破了什么:) 但它并没有立即崩溃。

    用一些东西初始化你的 top 变量:

    Stack *top;
    top = malloc(sizeof Stack);
    if (!top) {
        /* die */
    }
    

    【讨论】:

      【解决方案3】:

      您尚未为top 分配任何内存。分配内存将解决问题(完成后不要忘记释放它)。添加 const char * 可能只是通过在堆栈上放置另一个变量来掩盖问题(这是非常随机的,并且编译器特定于这实际上使问题看起来得到了解决)。

      【讨论】:

        【解决方案4】:

        Stack *top 更改为Stack *top = new Stack()

        #include <iostream>
        #include <cstring>
        #include <cctype>
        
        using namespace std;
        
        struct Stack {
            int value;
            Stack *next;
        };
        
        void push(Stack* top, int value) {
            Stack *ntop = new Stack;
            ntop->value = top->value;
            ntop->next = top->next;
            top->next = ntop;
            top->value = value;
        }
        
        int pop(Stack* top) {
            int val = top->value;
            top->value = top->next->value;
            top->next = top->next->next;
            return val;
        }
        
        int main()
        {
            Stack *top = new Stack();
            top->next = NULL;
            push(top, 20);
            cout << pop(top);
        
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-27
          • 2020-01-23
          • 1970-01-01
          相关资源
          最近更新 更多