简体   繁体   中英

Segmentation fault without “const char*”

i have some question. I try this code and recieve "Segmentation fault" error:

#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
Segmentation fault

but if i add const char* test = ""; before Stack *top; it works normal:

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

where my mistake?

The problem is here:

Stack *top;
top->next = NULL;

You are referencing an uninitialized pointer. That's undefined behavior. So anything can happen and it may not be consistent with surrounding code.

I suppose you forgot to actually allocate something for top .

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

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

    delete top;   //  Free

    return 0;
}

*Though I'd want point out that you'll still have memory leaks inside the code.

int main()
{
    Stack *top;
    top->next = NULL;

If this were raw C, you'd be writing a NULL into garbage location -- the top variable has not been initialized, so it points to garbage. The ->next will follow your garbage pointer and then write into it at an offset of 4 or 8 bytes away. Still garbage.

Maybe C++ does some magic struct == class magic initialization for you -- I don't know C++ well enough to comment -- but you're probably still looking at garbage.

Adding the test = "" changes the memory layout just enough so that you're overwriting something that is within your process's address space. It's still garbage, so who knows what you broke :) but it didn't immediately crash.

Initialize your top variable with something:

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

You have not allocated any memory for top . Allocating memory will fix the problem (don't forget to free it when done). Adding the const char * probably just masks the problem by placing another variable on the stack (it's very random and compiler specific that this actually makes the problem appear to be resolved).

Change Stack *top to 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM