0
struct a {
    int *val;
};

void main(){
    int n;
    struct a *a1;
    a1= malloc(sizeof(a1));

    n=10;

    a1->val = &n;

    func(a1);


    printf("After changing %d\n",a1->val);

}

void func(struct a *a2){
    int a = 5;
    a2->val = &a;
    a2->val = 0 ;
}

Assigned local variable to a member structure pointer. and finally making it null. instead of giving null pointer it is giving 0 when tried to access it.

Matt
  • 22,721
  • 17
  • 71
  • 112
shunty
  • 375
  • 2
  • 7
  • 24
  • 1
    Don't use `void main()` unless you like being shouted at for doing so. – Jonathan Leffler Jun 14 '12 at 07:06
  • `NULL` is technically `0`. Also, you don't set the pointer to `NULL` but to `19` in your example. – Some programmer dude Jun 14 '12 at 07:07
  • 1
    I don't see any "finally making it null", and the statement `a2->val = 19;` should result in a compile time error. You should post the code you're really asking about. It seems that your question is really just about the undefined behavior of trying to access a variable through a pointer after the lifetime of the variable has expired. – Michael Burr Jun 14 '12 at 07:07
  • 1
    It should be a1= malloc(sizeof(a)); And What is your question?? – Swanand Jun 14 '12 at 07:08
  • I am trying to assign a local variable to a pointer member of structure. Is it OK? Because scope of local variable is only the function. Should I make it null before exiting. – shunty Jun 14 '12 at 07:10
  • 1
    It is OK for the duration of the function; it is not OK once the function returns. On the whole, it is best to avoid the whole issue. However, if you must, then setting `a2->val = 0;` before return is cleaner than leaving it pointing at a local variable in the called function. It would be better still to reset the value to what it was on entry, but in that case, why bother with passing the structure at all. Given that this is close to minimal code, we can't see the justification for what you're doing. It is aconventional, even though not precisely wrong. It will easily lead to bugs, though. – Jonathan Leffler Jun 14 '12 at 07:16
  • Thanks for very good explanation. So never assign Local variable address to global or pass their address in function parameter. – shunty Jun 14 '12 at 07:24
  • oh don't forget to properly allocated your struct `a1 = malloc(sizeof(struct a));` `sizeof(a1));` will always return 4 or 8 depending on the architecture, no matter how many other members you add to your structure since a1 is a pointer. – Samy Vilar Jun 14 '12 at 08:03

1 Answers1

0

Since a1->val is pointing at an invalid address (19 is very seldom a valid address), you are invoking undefined behaviour and any result is acceptable (including a core dump or other crash).

Even if your function did:

*a2->val = 19;

the pointer would be pointing at an integer that is no longer valid when the function returns. Once the function has terminated, it is no longer safe to dereference the pointer; you could safely assign a new pointer value to it; you could compare the pointer to another pointer or NULL (subject to some constraints); but that's about all.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278