r/C_Programming • • Jul 16 '26

Why does it show me a segfault?

When I compile the code without any compiler flags, I see a segmentation fault. Why?

static int linked_init(struct list_t *list)
{
    if(linked_init)
        list->head = list->tail = NULL;
    else
        return -1;
    return 0;
}


int main()
{
    linked_init(NULL);
}

In the `init` function, I checked for a null pointer.

UPDATE: Sorry to everyone, I made a fucking bad mistake. My problem is solved.

38 Upvotes

25 comments sorted by

2

u/TPIRocks Jul 16 '26

You updated the post to tell us you fixed it, but not what is wrong, why would you do that?

3

u/Yousef_Tele Jul 16 '26

In the if statement, I set the wrong name (function name)

45

u/Modi57 Jul 16 '26

In your if condition, you check if linked_init is not null. It should be list instead

-12

u/DawnOnTheEdge Jul 16 '26 edited Jul 16 '26

To be language-lawyery, the C standard says “a null pointer, is guaranteed to compare unequal to a pointer to any object or function.” For a conversion to boolean, “the result is false if the value is [...] null (for pointer types), [...] otherwise, the result is true.” So the test always passes and might just be optimized out.

Edit: I should have said explicitly that nothing you said was wrong.

15

u/Modi57 Jul 16 '26

Is this an addition to my comment or a correction? Because I see no contradiction in our statements

1

u/DawnOnTheEdge Jul 16 '26 edited Jul 16 '26

An addition. I think the practical importance is that, with optimization on, the programmer should expect the comparison to be optimized out completely at runtime.

5

u/Modi57 Jul 17 '26

While true, I don't think it is that important, if it get's optimized out, if it's incorrect in the first place

-1

u/DawnOnTheEdge Jul 17 '26

In this case, there is a pragmatic take-away: if we aren’t getting a warning that the if statement is always true that also suggests we should be enabling more warnings. And in general, it’s useful to analyze what code does when determining whether it is correct or incorrect.

2

u/Modi57 Jul 17 '26

Yeah, totally. I'm all for enabling all warnings :)

20

u/RealisticDuck1957 Jul 16 '26

For future seg fault bugs:

- compile with debug symbols enabled.

- run inside a debugger. This will tell you exactly where in the code a seg fault is realized. And facilitate inspection of what's going on before that line of code executes.

13

u/umlcat Jul 16 '26

// you made a mistake:

static int linked_init(struct list_t *list)

{

if(list != NULL)

list->head = list->tail = NULL;

else

return -1;

return 0;

}

9

u/ReallyEvilRob Jul 16 '26
  1. In main(): You are passing NULL to linked_init().
  2. In linked_init(): Your conditional always evaluates to TRUE.
  3. This causes a NULL pointer assignment that results in the segfault.

2

u/Alduish Jul 16 '26

So in the if you put the wrong thing, you should put if(list != NULL).

Right now it's if(linked_list) with linked_list being a function so if the function pointer isn't NULL (which it isn't since it points to an existing function) you try to de-reference list which is NULL, that causes a segfault

2

u/grigus_ Jul 16 '26

You have a mistake. I would write if(list != NULL)

1

u/Ok_Chemistry_6387 Jul 16 '26

I am genuinely interested in why compiling with flags changed the behaviour. Were you compiling with optimisations previously?

1

u/Living_Fig_6386 Jul 20 '26

You can't dereference NULL. You're doing NULL->head = NULL->tail = NULL

Did you mean if (list) in the function?

-16

u/cbf1232 Jul 16 '26

You need to test the value of 'list', not 'linked_init'.

Next time ask an LLM first.

12

u/Skriblos Jul 16 '26

No, he can ask here.

-7

u/cbf1232 Jul 16 '26

Sure but asking an LLM will get an answer faster.  And unlike many other uses, in a case like this the answer is easy to verify.

15

u/Ok_Chemistry_6387 Jul 16 '26

Or they can come here, get multiple insights, a sense of belonging to a community.

-4

u/[deleted] Jul 16 '26

[removed] — view removed comment

5

u/OceanMachine101 Jul 16 '26

That is a pretty vague question. Start a new post with more specifics and someone can probably help.

1

u/Paul_Pedant Jul 17 '26

This sub gets about 75,000 visits per week, so there is probably an outside chance that at least one of us knows something.

Won't be me though, because I skip people who hide their posting history. Unless you feel like sharing your reasons for doing that? If I can't see your existing level of general knowledge from your previous posts, I have no idea how to explain anything in terms you might understand.