0

I created one struct and assigned one pointer variable to that structure. When I tried to print the address of that variable it prints correctly. After assign value to one of the member of the structure using this pointer structure variable, when i tried to print the address of the pointer variable I got segmentation fault.

#include <stdio.h>

typedef struct node {
char mem;
double mem2;
char mem3;
int mem4;
char mem5;
char mem6;
}NODE;
int main()
{
    NODE * m;
    printf("%u",&m);

    return 0;
}

I got output as 3014616488. When I assigned variable to one of the structure member,

#include <stdio.h>

typedef struct node {
char mem;
double mem2;
char mem3;
int mem4;
char mem5;
char mem6;
}NODE;
int main()
{
   NODE * m;
   m->mem3 = 'A';
   printf("%u",&m);

   return 0;
}

I got segmentation fault. When I did like this

#include <stdio.h>

typedef struct node {
char mem;
double mem2;
char mem3;
int mem4;
char mem5;
char mem6;
}NODE;
int main()
{
    NODE * m;
    printf("%u",&m->mem3);

    return 0;
}

I got 16 as output . I don't know how 16 came. It really struggling my mind. Please any one help me to get know what exactly happening. Thanks in advance

  • 2
    `m` is a pointer, but you never make it point anywhere. So when you dereference it (by doing `m->mem3`) you have [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). Uninitialized local variables will have an *indeterminate* (and seemingly random) value. – Some programmer dude Nov 20 '18 at 06:14
  • 2
    Also, when printing a pointer with [`printf`](https://en.cppreference.com/w/c/io/fprintf) you should use `"%p"`, otherwise you also have undefined behavior. And the pointer should really be casted to `void *` for it to be correct. Lastly, don't forget that `&m` is the pointer to the variable `m` (and have the type`NODE **`), it's not where `m` is pointing (that would be plain `m` without the address-of operator). – Some programmer dude Nov 20 '18 at 06:16
  • regarding the second version of the code: the variable `m` is never initialized to an instance of the struct Node. Rather an instance of a pointer to such a struct is declared. BUT that pointer is never set to point to an instance of the struct Node. So the statement: `m->mem3 = 'A';` is undefined behavior, where anything could happen. Note: all three versions of the posted code have the same kind of problem, resulting in undefined behavior – user3629249 Nov 20 '18 at 06:22
  • when compiling, always enable the warnings, then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -Werror -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same thing. – user3629249 Nov 20 '18 at 06:23
  • 3rd version of code is not undefined , it is valid one. But its in printing the start byte of the mem3 variable. I don't know why.Please explain this. @ user3629249 – Aadhithan eswaramoorthy Nov 20 '18 at 09:08
  • The third version is *also* UB, because the `->` operator dereferences the uninitialized pointer `m`. It's also a duplicate of [this one](https://stackoverflow.com/q/53390348/440558). Is it some class you're both going to? Online or in-person? Anyway, like I told the poster of the other question, please tell your class-mates to not post the same question again. – Some programmer dude Nov 20 '18 at 16:53

1 Answers1

2

Because its not allocated any memory

NODE * m = malloc(sizeof(NODE));

// Do stuffs

free(m);

So try this :

int main()
{
    NODE * m = malloc(sizeof(NODE));

    m->mem3 = 'A';
    printf("%u",&m->mem3);

    free(m);

    return 0;
}

Notice :

Int c or c++ if you declare a pointer, some memory is reserved for the pointer (usually on the stack). How ever the pointer is not initialized, meaning that it points to the address corresponding to what was in the respective memory location last. (This is the reason that first section of code is worked).