Re: Anyone want to look over my C code for me? I can't figure it out...

Discussion in 'C Programming' started by Arthur J. O'Dwyer, Sep 4, 2003.

  1. On Thu, 4 Sep 2003, pete wrote:
    >
    > Corey Murtagh wrote:
    > > Arthur J. O'Dwyer wrote:
    > > > On Thu, 4 Sep 2003, J Peterman wrote:

    > > <snip>
    > > >> char *ptr[NUMBER_OF_POINTERS]={NULL};
    > > >
    > > > I *think* this is valid, but I'd much prefer to see
    > > >
    > > > char *ptr[NUMBER_OF_POINTERS] = {0};
    > > >
    > > > because that's the accepted idiom.

    > >
    > > We
    > > had a discussion recently about that.


    I *think* you're referring to a discussion about whether
    memset(&ptr, 0, sizeof ptr);
    is guaranteed to null out the value of 'ptr'. No, it's
    not. But that's pretty obvious, isn't it?

    > > The consensus was that using 0 instead of NULL is undefined,
    > > on the grounds that for some arbitrary
    > > platform NULL may not equate to 0.

    >
    > (NULL == 0) is true on all platforms.


    Yes, pete, but this *was* the crux of my objection, in
    some sense. Because while the *expression* (NULL==0)
    is always true, sometimes the *token* NULL is not
    equivalent to the *token* 0. For example, what if
    our compiler has

    #define NULL (void *)0

    char *ptr[NUMBER_OF_POINTERS] = {(void *)0};

    Is this absolutely *guaranteed* to null all of the pointers
    in 'ptr'? I'm pretty sure it is, via N869 section 6.7.8#21,
    but I had to look it up. I don't think there's any reason
    to avoid the natural idiom

    = {0};

    for "zeroing" an array. (Which, also via 6.7.8#21, doesn't
    actually perform a 'memset' -- it initializes all remaining
    members "the same as objects that have static storage
    duration." So it's also okay.)


    > > Personally I agree with you, but it
    > > seems the standard doesn't agree with /us/ *shrug*

    >
    > Where does the standard say that ?
    >
    > Followup set to comp.lang.c


    Okay. But since I hope this'll put this thread
    to rest, I'm copying to comp.programming one
    more time.

    -Arthur
    Arthur J. O'Dwyer, Sep 4, 2003
    #1
    1. Advertising

  2. Arthur J. O'Dwyer wrote:

    <snip>

    > For example, what if
    > our compiler has
    >
    > #define NULL (void *)0
    >
    > char *ptr[NUMBER_OF_POINTERS] = {(void *)0};
    >
    > Is this absolutely *guaranteed* to null all of the pointers
    > in 'ptr'?


    Yes. ptr[0] gets the value (void *)0, and every other element in the array
    gets the default static initialiser value for pointers, which is NULL.

    > I'm pretty sure it is, via N869 section 6.7.8#21,
    > but I had to look it up. I don't think there's any reason
    > to avoid the natural idiom
    >
    > = {0};
    >
    > for "zeroing" an array.


    Neither do I see any reason to avoid this.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Sep 4, 2003
    #2
    1. Advertising

  3. Re: Anyone want to look over my C code for me? I can't figure itout...

    Arthur J. O'Dwyer wrote:

    > On Thu, 4 Sep 2003, pete wrote:
    >
    >>Corey Murtagh wrote:
    >>
    >>Arthur J. O'Dwyer wrote:

    <snip>
    >>>I *think* this is valid, but I'd much prefer to see
    >>>
    >>> char *ptr[NUMBER_OF_POINTERS] = {0};
    >>>
    >>>because that's the accepted idiom.

    >>
    >>We
    >>had a discussion recently about that.

    >
    > I *think* you're referring to a discussion about whether
    > memset(&ptr, 0, sizeof ptr);
    > is guaranteed to null out the value of 'ptr'. No, it's
    > not. But that's pretty obvious, isn't it?


    Ahh... memory failed me. Yes, that was the discussion. Sorry for any
    confusion I've caused.

    --
    Corey Murtagh
    The Electric Monk
    "Quidquid latine dictum sit, altum viditur!"
    Corey Murtagh, Sep 5, 2003
    #3
  4. Re: Anyone want to look over my C code for me? I can't figure it out...

    Richard Heathfield <> wrote in message news:<bj8f4m$had$>...
    > Arthur J. O'Dwyer wrote:
    >
    > <snip>
    >
    > > For example, what if
    > > our compiler has
    > >
    > > #define NULL (void *)0
    > >
    > > char *ptr[NUMBER_OF_POINTERS] = {(void *)0};
    > >
    > > Is this absolutely *guaranteed* to null all of the pointers
    > > in 'ptr'?

    >
    > Yes. ptr[0] gets the value (void *)0, and every other element in the array
    > gets the default static initialiser value for pointers, which is NULL.


    You've just pointed out that there may be multiple forms of null
    pointers, so the initialiser needn't be constructed as if was from
    NULL explicitly, but the default initialiser must certainly be a null
    pointer.

    --
    Peter
    Peter Nilsson, Sep 5, 2003
    #4
  5. Re: Anyone want to look over my C code for me? I can't figure it out...

    Peter Nilsson wrote:

    <snip>

    > You've just pointed out that there may be multiple forms of null
    > pointers, so the initialiser needn't be constructed as if was from
    > NULL explicitly, but the default initialiser must certainly be a null
    > pointer.


    A null pointer constant, yes.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Sep 5, 2003
    #5
  6. On Fri, 5 Sep 2003, pete wrote:
    >
    > Arthur J. O'Dwyer wrote:
    > > On Thu, 4 Sep 2003, pete wrote:
    > > > Corey Murtagh wrote:


    > > > > on the grounds that for some arbitrary
    > > > > platform NULL may not equate to 0.
    > > >
    > > > (NULL == 0) is true on all platforms.

    > >
    > > Yes, pete, but this *was* the crux of my objection, in
    > > some sense.

    >
    > I challenged Corey Murtagh's claims.
    > I'm on *your* side.


    Yeah, I know. The crux of my objection to J Peterman's
    code was that while NULL is *equal* to 0, it's not always
    *equivalent* (trivial example -- you can't expect these
    lines to behave identically:

    int i = 0;
    int i = NULL;

    ). But it turns out that in the context of that part of
    J Peterman's code, the two *are* equivalent. So anyway,
    that's enough in this thread. I think everyone gets it
    now.

    -Arthur
    Arthur J. O'Dwyer, Sep 5, 2003
    #6
  7. Arthur J. O'Dwyer wrote:

    >
    > The crux of my objection to J Peterman's
    > code was that while NULL is *equal* to 0, it's not always
    > *equivalent* (trivial example -- you can't expect these
    > lines to behave identically:
    >
    > int i = 0;
    > int i = NULL;


    That is true. But then who would expect them to? We don't use NULL, which
    represents a null pointer constant, in int contexts. In a *pointer*
    context, however:

    int *p = 0;

    and

    int *p = NULL;

    are interchangeable.


    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Sep 6, 2003
    #7
    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. Programmer Dude
    Replies:
    10
    Views:
    528
    Programmer Dude
    Sep 10, 2003
  2. Steve
    Replies:
    0
    Views:
    525
    Steve
    Sep 25, 2006
  3. Taras_96
    Replies:
    3
    Views:
    331
    Taras_96
    Jan 29, 2008
  4. Matthew
    Replies:
    3
    Views:
    85
    Matthew
    Feb 10, 2004
  5. Simon Egginton

    LOOK! i just want to know does anyone know...

    Simon Egginton, Jul 26, 2004, in forum: Javascript
    Replies:
    3
    Views:
    176
    Dr John Stockton
    Jul 26, 2004
Loading...

Share This Page