0

I wrote a C program. Some part of the code which is inside a function looks like this:

struct node* functionName(struct node *currentFirstPointer){
    struct node **head = NULL;
    *head = currentFirstPointer;
    return *head;
}

Here node is a structure. But this line gives me a segmentation fault when I run the program. But if I declare and initialize the pointer to pointer in separate statements inside the same function like below then it works fine.

struct node* functionName(struct node *currentFirstPointer){
    struct node **head;
    *head = NULL;
    *head = currentFirstPointer;
    return *head;
}

What could be the reason that the 1st block doesn't work and the 2nd block works fine?

Kaustav
  • 741
  • 1
  • 9
  • 18
  • 1
    You mind creating a [___MCVE___](http://stackoverflow.com/help/mcve)? – Sourav Ghosh Aug 29 '16 at 06:16
  • 2
    Are you sure that the 1st example works and the 2nd doesn't? Because that doesn't make any sense. Didn't you mean the other way around? If the 1st example is causing seg fault then the problem isn't in the code posted. – Lundin Aug 29 '16 at 06:20
  • I understand that it's an undefined behavior in your case, but can you please tell me which compiler are you using for this because I cannot reproduce this on Visual Studio. – The Apache Aug 29 '16 at 06:21
  • The second works. But the 1st dosen't – Kaustav Aug 29 '16 at 06:22
  • the compiler is gcc – Kaustav Aug 29 '16 at 06:23
  • 2nd block should also produce undefined behavior. – Shravan40 Aug 29 '16 at 06:23
  • But 2nd block works fine – Kaustav Aug 29 '16 at 06:23
  • @Kaustav The second does most definitely not work. It may however _seem_ to work, because you are invoking undefined behavior. The problem with the 1st isn't in the code posted though. – Lundin Aug 29 '16 at 06:23
  • 1
    Please edit the question and show the context where the statements appear. In the first example, is the code inside or outside a function? In the second example with two statements, are they both inside a function, or both outside, or is one inside and the other outside (and which)? What are you doing to get the core dump? Your observation doesn't tie into what people expect. – Jonathan Leffler Aug 29 '16 at 06:24
  • I have edited the question. The block is inside a function – Kaustav Aug 29 '16 at 06:27
  • The first line should not cause a crash on its own. It must be something that you're doing after that line is executed that causes the crash. Without the rest of the code — or, at least, every other use of `head` in the function — we can't guess what you're doing wrong. If you have debug printing, did you include newlines at the end of the format strings? Did you invoke `fflush(0);` liberally? The second code is undefined behaviour because you've not initialized `head` to point anywhere, so using `*head` invokes undefined behaviour. One undefined behaviour is 'seems to work; does not crash'. – Jonathan Leffler Aug 29 '16 at 06:30

1 Answers1

1

You have two examples of dereferencing a pointer.

struct node **head = NULL;
*head = currentFirstPointer;

and

struct node **head;
*head = NULL;
*head = currentFirstPointer;

Both are cause for undefined behavior. In the first, you are dereferencing a NULL pointer. In the second, you are dereferencing an uninitialized pointer.

The second block may appear to work but that's the problem with undefined behavior.

You need to allocate memory for head first before you can dereference the pointer.

struct node **head = malloc(sizeof(*head)*SOME_COUNT);
*head = currentFirstPointer;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    The OP claims that the 2nd example works and the 1st doesn't. – Lundin Aug 29 '16 at 06:18
  • @Lundin, I am saying even the second block is cause for undefined behavior. It may appear to work but it is undefined behavior. – R Sahu Aug 29 '16 at 06:20
  • 1
    That doesn't explain why the 1st one gives a seg fault though. The question is unclear and cannot be answered without further clarification. – Lundin Aug 29 '16 at 06:21
  • @Lundin, both are cause for undefined behavior. We can't try to make sense of undefined behavior. – R Sahu Aug 29 '16 at 06:23
  • The 1st example is just a perfectly fine initialization. – Lundin Aug 29 '16 at 06:24
  • 1
    @Lundin, Dereferencing a NULL pionter as well as uninitialized pointer is cause for undefined behavior. The undefined behavior manifests differently. – R Sahu Aug 29 '16 at 06:26
  • @Kaustav, seemingly sane behavior is, unfortunately, is also part of undefined behavior. – R Sahu Aug 29 '16 at 06:27
  • @RSahu My point is, the 1st example is just an initialization, there is no code posted showing that he de-references it. It may seem likely that's the case, but we can't tell from the question. – Lundin Aug 29 '16 at 06:28
  • @Lundin, I misunderstood. `struct node **head = NULL:` should not cause undefined behavior. Point taken. – R Sahu Aug 29 '16 at 06:29
  • @Kaustav, please post a [mcve] in a hurry before the question gets closed. – R Sahu Aug 29 '16 at 06:30
  • @R Sahu Got my answer – Kaustav Aug 29 '16 at 06:36