Access NULL pointer

Discussion in 'C Programming' started by Tagore, Jul 23, 2010.

  1. Tagore

    Tagore Guest

    Hi,

    I was trying to debug a project.
    I found that pointer to one of struct record was NULL, but it was
    still able to dereference and take some garbage value.
    i.e. if foo is a pointer whose value is NULL. then when I try to
    access a data member like foo->bar..it is still able to take some
    garbage value of bar despite foo being NULL.
    How is it possible? Should not it crash here?

    Thanks,
     
    Tagore, Jul 23, 2010
    #1
    1. Advertising

  2. Tagore

    Ian Collins Guest

    On 07/23/10 12:10 PM, Tagore wrote:
    > Hi,
    >
    > I was trying to debug a project.
    > I found that pointer to one of struct record was NULL, but it was
    > still able to dereference and take some garbage value.
    > i.e. if foo is a pointer whose value is NULL. then when I try to
    > access a data member like foo->bar..it is still able to take some
    > garbage value of bar despite foo being NULL.
    > How is it possible? Should not it crash here?


    It depends. That's the joy of undefined behaviour.

    If the address offsetof( record, bar ) is valid, it might not crash.

    --
    Ian Collins
     
    Ian Collins, Jul 23, 2010
    #2
    1. Advertising

  3. Tagore <> wrote:
    > I was trying to debug a project.
    > I found that pointer to one of struct record was NULL,
    > but it was still able to dereference and take some
    > garbage value. i.e. if foo is a pointer whose value is
    > NULL. then when I try to access a data member like
    > foo->bar..it is still able to take some garbage value
    > of bar despite foo being NULL.
    > How is it possible? Should not it crash here?


    Did you miss the thread titled 'C Standard Regarding Null
    Pointer Dereferencing'?

    Undefined behaviour means *anything* can happen, including
    the illusion that everything works just fine.

    --
    Peter
     
    Peter Nilsson, Jul 23, 2010
    #3
  4. Tagore wrote:
    > How is it possible? Should not it crash here?


    There's no such thing as "crash" in C language. What it has instead is
    called "undefined behavior". Undefined behavior can manifest itself in
    many different ways, none of which is better that the others. The
    program might "crash", or the program might return a "garbage value". As
    far as the language is concerned, there's no difference between
    "crashing" and "returning garbage".

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jul 23, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    27,588
    Mike Schilling
    Mar 29, 2006
  2. Ken
    Replies:
    69
    Views:
    12,678
    Michael D. Borghardt
    Jul 31, 2004
  3. Ken
    Replies:
    5
    Views:
    2,589
    Victor Bazarov
    Jul 24, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    784
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    468
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page