NULL pointers

Discussion in 'C Programming' started by Ark Khasin, Jan 17, 2008.

  1. Ark Khasin

    Ark Khasin Guest

    Consider, at file scope,
    struct {void *foo, *bar;} X = {.foo=NULL,};
    bar will get all-bits-zero which AFAIK officially ain't necessarily a NULL.
    Now, how bad is the assumption that X.bar==NULL? Are there any platforms
    where this is wrong?
    Thanks,
    --
    Ark
    Ark Khasin, Jan 17, 2008
    #1
    1. Advertising

  2. Ark Khasin

    Mark Bluemel Guest

    Ark Khasin wrote:
    > Consider, at file scope,
    > struct {void *foo, *bar;} X = {.foo=NULL,};
    > bar will get all-bits-zero which AFAIK officially ain't necessarily a NULL.
    > Now, how bad is the assumption that X.bar==NULL? Are there any platforms
    > where this is wrong?


    Implicit addition to your question - "and the initialization syntax I
    used is valid".

    I'll let others tell you of platforms where all-bits zero isn't a NULL.

    I think I'll also leave them to point out the dangers of implementing
    code which just happens to work rather than being guaranteed to do so.
    Mark Bluemel, Jan 17, 2008
    #2
    1. Advertising

  3. Ark Khasin said:

    > Consider, at file scope,
    > struct {void *foo, *bar;} X = {.foo=NULL,};
    > bar will get all-bits-zero


    Probably. In fact, it will be initialised to NULL, as required by the
    default static initialisation rules. If NULL is all-bits-zero, then yes,
    it'll get all-bits-zero. If NULL is SYS:$*3:X42J, it'll get SYS:$*3:X42J.
    If NULL is Canterbury Cathedral, it'll get Canterbury Cathedral.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Jan 17, 2008
    #3
  4. Ark Khasin

    Kaz Kylheku Guest

    On Jan 17, 1:18 am, Ark Khasin <> wrote:
    > Consider, at file scope,
    > struct {void *foo, *bar;} X = {.foo=NULL,};
    > bar will get all-bits-zero


    That is incorrect. Bar will be initialized to null, just like foo,
    whose initializer is redundant since X is a static object.

    Static objects with no explicit initializers are are given proper zero
    values of their type. Pointers are null, floating-point objects have
    value 0.0, and integers are 0.

    Even if you move X to a block scope and give it automatic instead of
    static storage, both foo and bar will still be initialized to null.
    That's because aggregate such as a structure or array is either fully
    initialized, or not at all; there is no partial initialization. If any
    member has an initializer, whether designated or classic style, all of
    the other members are also initialized. Any members which don't have
    an initializer get a zero.

    {
    // no members are initialized
    struct s a;

    // first member gets explicit zero
    // all other members, implicit zero
    struct s b = { 0 };

    // ...
    }

    > which AFAIK officially ain't necessarily a NULL.


    You mean ``null''. ``NULL'' is a preprocessor symbol in C.

    > Now, how bad is the assumption that X.bar==NULL?


    It's a half-decent assumption as far as assumptions go.
    Kaz Kylheku, Jan 17, 2008
    #4
  5. Ark Khasin

    Ark Khasin Guest

    Richard Heathfield wrote:
    > Ark Khasin said:
    >
    >> Consider, at file scope,
    >> struct {void *foo, *bar;} X = {.foo=NULL,};
    >> bar will get all-bits-zero

    >
    > Probably. In fact, it will be initialised to NULL, as required by the
    > default static initialisation rules. If NULL is all-bits-zero, then yes,
    > it'll get all-bits-zero. If NULL is SYS:$*3:X42J, it'll get SYS:$*3:X42J.
    > If NULL is Canterbury Cathedral, it'll get Canterbury Cathedral.
    >

    Oops. My bad, and thank you. [I must be sleeping instead of posting.] Sorry.
    --
    Ark
    Ark Khasin, Jan 17, 2008
    #5
  6. Ark Khasin

    James Kuyper Guest

    Ark Khasin wrote:
    > Consider, at file scope,
    > struct {void *foo, *bar;} X = {.foo=NULL,};
    > bar will get all-bits-zero which AFAIK officially ain't necessarily a NULL.


    Bar will be initialized to a null pointer (6.7.8p10). That would be true
    even if X did not have static storage duration (6.7.8p19). However,
    there's no guarantee that it will have all bits zero. Despite that fact,
    you're correct that it isn't necessarily NULL. NULL is a macro whose
    expansion is required to be a null pointer constant, and would therefore
    produce a null pointer if it were assigned to bar, but not necessarily
    the same null pointer that results from bar being implicitly initialized.

    > Now, how bad is the assumption that X.bar==NULL?


    It's a perfectly valid assumption. All null pointers, regardless of
    representation, are required to compare equal to each other and to all
    null pointer constants.
    James Kuyper, Jan 17, 2008
    #6
    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. Kivak Wolf
    Replies:
    2
    Views:
    10,064
    Kivak Wolf
    Jun 28, 2005
  2. vizlab
    Replies:
    3
    Views:
    4,196
    Michael Bar-Sinai
    Oct 17, 2007
  3. Replies:
    16
    Views:
    7,317
    Mike Schilling
    Oct 12, 2005
  4. Replies:
    5
    Views:
    26,595
    Mike Schilling
    Mar 29, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    668
Loading...

Share This Page