Thread: Dereferencing mystery?

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    952

    Dereferencing mystery?

    I find it strange that NULL is passed to printf below. Since p is NULL, it seems more likely that p->s would blow up before the function call.
    Code:
    #include <stdio.h>
     
    typedef struct {
        char s[100];
    } Object;
     
    int main() {
        Object *p = NULL;
        printf("%s\n", p->s);   // prints (null)
        return 0;
    }
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    729
    Since this is undefined behavior, the compiler can do anything it wants to do, even including printing "(null)".

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    410
    Quote Originally Posted by john.c View Post
    I find it strange that NULL is passed to printf below. Since p is NULL, it seems more likely that p->s would blow up before the function call.
    Code:
    #include <stdio.h>
     
    typedef struct {
        char s[100];
    } Object;
     
    int main() {
        Object *p = NULL;
        printf("%s\n", p->s);   // prints (null)
        return 0;
    }

    s is the first member of struct "Object". So the address of the member is constrained to be the same as the address of the structure. You are treating NULL as the base address of the Object pointed to by p, and taking the address of the array s, which is also constrained to be null.
    If printf() is documented as handling a NULL argument to %s, it is not actually undefined behaviour.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    952
    @christop, Obviously it's undefined behavior. That's not the point. And I have no problem with printf printing "(null)". The point is how does the printf get called at all? Why doesn't it blow up as soon as the p->s is executed (i.e., obviously before the printf call)? Remember that p->s means (*p).s. One might expect that *p would blow up, since it is dereferencing NULL.

    But because it's "undefined behavior" to dereference a NULL the implementation is allowed to just assume p is not NULL, notice that s is the first member, and simply pass the value of p cast to the type of s.

    So the answer has to do with char s[100] being the first member in the struct, as Malcolm points out. Move it to second place and it segfaults.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    729
    Ah, I think Malcolm hit the nail on the head. I overlooked that it's an array. It's an array so it "decays" to a pointer to the first element of the array, and since it's the first field in the struct it has the same address as the struct itself.

    But dereferencing a NULL pointer like that is still undefined behavior, AFAIK, that just happens to give you "expected" results.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    410
    Quote Originally Posted by christop View Post
    Ah, I think Malcolm hit the nail on the head. I overlooked that it's an array. It's an array so it "decays" to a pointer to the first element of the array, and since it's the first field in the struct it has the same address as the struct itself.

    But dereferencing a NULL pointer like that is still undefined behavior, AFAIK, that just happens to give you "expected" results.
    To be fair, you'd have to go through the C standard chapter and verse to see whether it is defined or not. It's clearly buggy code.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    698
    Testing I've got a "segmentation fault" here...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ceil mystery
    By Drogin in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 01:24 PM
  2. mystery seg fault o' tha day
    By MK27 in forum C Programming
    Replies: 6
    Last Post: 01-26-2009, 03:06 AM
  3. MAKEINTRESOURCE mystery
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 09-03-2003, 08:06 AM
  4. Mystery.
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 01:41 AM
  5. The great mystery
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 08-15-2001, 08:08 PM

Tags for this Thread