NULL pointer and zero value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack Klein

    #16
    Re: NULL pointer and zero value

    On Tue, 30 Dec 2003 14:07:21 GMT, pete <pfiland@mindsp ring.com> wrote
    in comp.lang.c:
    [color=blue]
    > Martin Dickopp wrote:[color=green]
    > >
    > > dt1649651@yahoo .com (vp) writes:
    > >[color=darkred]
    > > > If I have a pointer char * p, is it correct to assign NULL to this
    > > > pointer by:
    > > >
    > > > "memset( &p, 0, sizeof(p));" instead of "p = NULL;"[/color]
    > >
    > > No. The former statement sets all bits of `p' to zero, which is not
    > > necessarily a representation of the null pointer.
    > >[color=darkred]
    > > > The reason I ask is I have an array of structure of N function
    > > > variables, for example,
    > > >
    > > > typedef struct
    > > > {
    > > > int (* func1)();
    > > > int (* func2)();
    > > > void * (* func2)(int );
    > > > } ModuleFunctions ;
    > > >
    > > > #define N 100
    > > > ModuleFunction garMF[N];
    > > >
    > > > When initializing, instead of making a loop to assgin NULL to each
    > > > function member of struct ModuleFunction
    > > > for each member of array, is it correct to do something like
    > > >
    > > > memset( garMF, 0, sizeof(ModuleFu nctions)*N );[/color][/color]
    >[color=green]
    > > Otherwise (if `garMF' doesn't have static storage duration)
    > > you'll have to loop.[/color]
    >
    > I disagree about the necessity of a loop.
    >
    > ModuleFunction garMF[N] = {NULL};[/color]

    I would much prefer {0} to {NULL} here.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

    Comment

    • Kevin Goodsell

      #17
      Re: NULL pointer and zero value

      Mark A. Odell wrote:
      [color=blue]
      >
      > So are you saying that this isn't safe?
      >
      > #include <stdlib.h>
      >
      > int main(void)
      > {
      > int *pVar = malloc(1024);
      >
      > if (pVar) /* <--- Not safe? */
      > {
      > free(pVar);
      > }
      >
      > return EXIT_SUCCESS;
      > }
      >[/color]

      Answers already given, but I wanted to mention that this is also valid:

      int *p = 0;

      regardless of the object-representation of the null pointer value. This
      is somewhat obvious, because even on systems where the null-pointer
      value is not represented by all-bits-zero, NULL must still be defined as
      a constant expression with the value 0, possibly cast to void*, so this:

      int *p = NULL;

      is translated by the preprocessor to something equivalent to one of the
      following:

      int *p = 0;

      or

      int *p = (void *)0;

      It follows somewhat logically that things like these:

      if (p)
      if (p == 0)

      are actually testing p against the null pointer value, whatever its
      representation is.

      -Kevin
      --
      My email address is valid, but changes periodically.
      To contact me please use the address from a recent posting.

      Comment

      • Kevin Goodsell

        #18
        Re: NULL pointer and zero value

        Jack Klein wrote:
        [color=blue]
        > On Tue, 30 Dec 2003 14:07:21 GMT, pete <pfiland@mindsp ring.com> wrote
        > in comp.lang.c:
        >[color=green]
        >>
        >>ModuleFunctio n garMF[N] = {NULL};[/color]
        >
        >
        > I would much prefer {0} to {NULL} here.
        >[/color]

        Is {NULL} even correct here if NULL happens to be #defined with a cast
        to void*?

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • Peter Nilsson

          #19
          Re: NULL pointer and zero value

          "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
          news:ZAmIb.2649 5$Pg1.22006@new sread1.news.pas .earthlink.net. ..[color=blue]
          > Jack Klein wrote:
          >[color=green]
          > > On Tue, 30 Dec 2003 14:07:21 GMT, pete <pfiland@mindsp ring.com> wrote
          > > in comp.lang.c:
          > >[color=darkred]
          > >>
          > >>ModuleFunctio n garMF[N] = {NULL};[/color]
          > >
          > >
          > > I would much prefer {0} to {NULL} here.
          > >[/color]
          >
          > Is {NULL} even correct here if NULL happens to be #defined with a cast
          > to void*?[/color]

          Yes. Since NULL is a null pointer constant.

          6.3.2.3p4: "Conversion of a null pointer to another pointer type yields a
          null pointer of that type. ...". [There is no limitation with respect to
          function pointers in this regard.]

          6.7.8p11 Initialization: "...the same type constraints and conversions as
          for simple assignment apply,..."

          6.5.16.1p1 Simple Assignment - Constraints: "... - the left operand is a
          pointer and the right is a null pointer constant; ..."

          6.5.16.1p2 Simple Assignment - Semantics: "In simple assignment (=), the
          value of the right operand is converted to the type of the assignment
          expression and replaces the value stored in the object designated by the
          left operand."

          I think Jack prefers {0} because it's a more consistent (and well
          understood) paradigm in general.

          --
          Peter


          Comment

          • pete

            #20
            Re: NULL pointer and zero value

            Jack Klein wrote:[color=blue]
            >
            > On Tue, 30 Dec 2003 14:07:21 GMT,
            > pete <pfiland@mindsp ring.com wrote in comp.lang.c:[/color]
            [color=blue][color=green][color=darkred]
            > > > > typedef struct
            > > > > {
            > > > > int (* func1)();
            > > > > int (* func2)();
            > > > > void * (* func2)(int );
            > > > > } ModuleFunctions ;
            > > > >
            > > > > #define N 100
            > > > > ModuleFunction garMF[N];[/color][/color][/color]
            [color=blue][color=green]
            > > ModuleFunction garMF[N] = {NULL};[/color]
            >
            > I would much prefer {0} to {NULL} here.[/color]

            All of the members of all of the elements of the array
            will be initialized to NULL. Why do you like {0} better ?

            --
            pete

            Comment

            • Jack Klein

              #21
              Re: NULL pointer and zero value

              On Wed, 31 Dec 2003 12:46:37 GMT, pete <pfiland@mindsp ring.com> wrote
              in comp.lang.c:
              [color=blue]
              > Jack Klein wrote:[color=green]
              > >
              > > On Tue, 30 Dec 2003 14:07:21 GMT,
              > > pete <pfiland@mindsp ring.com wrote in comp.lang.c:[/color]
              >[color=green][color=darkred]
              > > > > > typedef struct
              > > > > > {
              > > > > > int (* func1)();
              > > > > > int (* func2)();
              > > > > > void * (* func2)(int );
              > > > > > } ModuleFunctions ;
              > > > > >
              > > > > > #define N 100
              > > > > > ModuleFunction garMF[N];[/color][/color]
              >[color=green][color=darkred]
              > > > ModuleFunction garMF[N] = {NULL};[/color]
              > >
              > > I would much prefer {0} to {NULL} here.[/color]
              >
              > All of the members of all of the elements of the array
              > will be initialized to NULL. Why do you like {0} better ?[/color]

              I should have elaborated. {0} works in all cases to initialize any
              static or automatic scalar or aggregate type.

              The C standard allows two definitions for the macro NULL. For
              simplicity's sake, they are "0" and "(void *)0", or equivalent. My
              experience is that most (but definitely not all) C compilers use the
              cast to (void *) rather than the raw numeric constant.

              In this case, since the first element of the structure has pointer
              type, either definition of NULL will work. If the structure had a
              non-pointer first member, it would be correct on implementations that
              used a raw 0 for NULL, but be a constraint violation on those that
              defined NULL as "(void *)0".

              --
              Jack Klein
              Home: http://JK-Technology.Com
              FAQs for
              comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
              comp.lang.c++ http://www.parashift.com/c++-faq-lite/
              alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

              Comment

              • Jack Klein

                #22
                Re: NULL pointer and zero value

                On Tue, 30 Dec 2003 22:01:29 GMT, Kevin Goodsell
                <usenet1.spamfr ee.fusion@never box.com> wrote in comp.lang.c:
                [color=blue]
                > Jack Klein wrote:
                >[color=green]
                > > On Tue, 30 Dec 2003 14:07:21 GMT, pete <pfiland@mindsp ring.com> wrote
                > > in comp.lang.c:
                > >[color=darkred]
                > >>
                > >>ModuleFunctio n garMF[N] = {NULL};[/color]
                > >
                > >
                > > I would much prefer {0} to {NULL} here.
                > >[/color]
                >
                > Is {NULL} even correct here if NULL happens to be #defined with a cast
                > to void*?[/color]

                I should have elaborated. {0} works in all cases to initialize any
                static or automatic scalar or aggregate type.

                The C standard allows two definitions for the macro NULL. For
                simplicity's sake, they are "0" and "(void *)0", or equivalent. My
                experience is that most (but definitely not all) C compilers use the
                cast to (void *) rather than the raw numeric constant.

                In this case, since the first element of the structure has pointer
                type, either definition of NULL will work. If the structure had a
                non-pointer first member, it would be correct on implementations that
                used a raw 0 for NULL, but be a constraint violation on those that
                defined NULL as "(void *)0".

                --
                Jack Klein
                Home: http://JK-Technology.Com
                FAQs for
                comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

                Comment

                • stau

                  #23
                  Re: NULL pointer and zero value

                  On Wed, 31 Dec 2003 15:37:42 +1100, Peter Nilsson wrote:
                  [color=blue]
                  > I think Jack prefers {0} because it's a more consistent (and well
                  > understood) paradigm in general.[/color]

                  No, he explained why,and it makes logic.

                  Comment

                  • Peter Nilsson

                    #24
                    Re: NULL pointer and zero value

                    "stau" <stau@pretogal. pt> wrote in message
                    news:pan.2004.0 1.01.01.09.00.1 02931@pretogal. pt...[color=blue]
                    > On Wed, 31 Dec 2003 15:37:42 +1100, Peter Nilsson wrote:
                    >[color=green]
                    > > I think Jack prefers {0} because it's a more consistent (and well
                    > > understood) paradigm in general.[/color]
                    >
                    > No, he explained why,and it makes logic.[/color]

                    How is "{0} works in all cases..." significantly different to what I said?
                    It's more succinct and better phrased, I grant you, but I don't see how my
                    sentence runs contrary to it.

                    P.S. Jack's replies were not available on my news server at the time I made
                    the post.

                    --
                    Peter


                    Comment

                    • pete

                      #25
                      Re: NULL pointer and zero value

                      Jack Klein wrote:[color=blue]
                      >
                      > On Wed, 31 Dec 2003 12:46:37 GMT, pete <pfiland@mindsp ring.com> wrote
                      > in comp.lang.c:
                      >[color=green]
                      > > Jack Klein wrote:[color=darkred]
                      > > >
                      > > > On Tue, 30 Dec 2003 14:07:21 GMT,
                      > > > pete <pfiland@mindsp ring.com wrote in comp.lang.c:[/color]
                      > >[color=darkred]
                      > > > > > > typedef struct
                      > > > > > > {
                      > > > > > > int (* func1)();
                      > > > > > > int (* func2)();
                      > > > > > > void * (* func2)(int );
                      > > > > > > } ModuleFunctions ;
                      > > > > > >
                      > > > > > > #define N 100
                      > > > > > > ModuleFunction garMF[N];[/color]
                      > >[color=darkred]
                      > > > > ModuleFunction garMF[N] = {NULL};
                      > > >
                      > > > I would much prefer {0} to {NULL} here.[/color]
                      > >
                      > > All of the members of all of the elements of the array
                      > > will be initialized to NULL. Why do you like {0} better ?[/color]
                      >
                      > I should have elaborated. {0} works in all cases to initialize any
                      > static or automatic scalar or aggregate type.[/color]

                      What you said, could also be used to explain a preference for
                      char *pointer = 0;
                      over
                      char *pointer = NULL;

                      Is that your preference also ?

                      --
                      pete

                      Comment

                      • Peter Pichler

                        #26
                        Re: NULL pointer and zero value

                        "pete" <pfiland@mindsp ring.com> wrote...[color=blue]
                        > Jack Klein wrote:[color=green]
                        > >
                        > > I should have elaborated. {0} works in all cases to initialize any
                        > > static or automatic scalar or aggregate type.[/color]
                        >
                        > What you said, could also be used to explain a preference for
                        > char *pointer = 0;
                        > over
                        > char *pointer = NULL;[/color]

                        Not quite. Since a void* is implicitly converted to any type*, the two
                        examples are exactly equivalent regardless of any of the two standard
                        definitions of NULL.
                        [color=blue]
                        > Is that your preference also ?[/color]

                        My preference is the latter, but it is only a style issue, nothing else.


                        Comment

                        • pete

                          #27
                          Re: NULL pointer and zero value

                          Peter Pichler wrote:[color=blue]
                          >
                          > "pete" <pfiland@mindsp ring.com> wrote...[color=green]
                          > > Jack Klein wrote:[color=darkred]
                          > > >
                          > > > I should have elaborated.
                          > > > {0} works in all cases to initialize any
                          > > > static or automatic scalar or aggregate type.[/color]
                          > >
                          > > What you said, could also be used to explain a preference for
                          > > char *pointer = 0;
                          > > over
                          > > char *pointer = NULL;[/color]
                          >
                          > Not quite.
                          > Since a void* is implicitly converted to any type*, the two
                          > examples are exactly equivalent regardless of any of the two standard
                          > definitions of NULL.[/color]

                          Exactly quite.

                          ModuleFunction garMF[N] = {NULL};
                          and
                          ModuleFunction garMF[N] = {0};

                          are exactly equivalent regardless of any of the two standard
                          definitions of NULL.

                          [color=blue][color=green]
                          > > Is that your preference also ?[/color]
                          >
                          > My preference is the latter,
                          > but it is only a style issue, nothing else.[/color]

                          Style issues are on topic.
                          Reasons for prefering one style over another
                          which involve legibility and maintainability ,
                          are the more interesting ones.

                          --
                          pete

                          Comment

                          • Peter Pichler

                            #28
                            Re: NULL pointer and zero value

                            "pete" <pfiland@mindsp ring.com> wrote...[color=blue]
                            > Jack Klein wrote:[color=green]
                            > >
                            > > I should have elaborated. {0} works in all cases to initialize any
                            > > static or automatic scalar or aggregate type.[/color]
                            >
                            > What you said, could also be used to explain a preference for
                            > char *pointer = 0;
                            > over
                            > char *pointer = NULL;[/color]

                            Not quite. Since a void* is implicitly converted to any type*, the two
                            examples are exactly equivalent regardless of any of the two standard
                            definitions of NULL.
                            [color=blue]
                            > Is that your preference also ?[/color]

                            My preference is the latter, but it is only a style issue, nothing else.


                            Comment

                            • Peter Pichler

                              #29
                              Re: NULL pointer and zero value

                              "pete" <pfiland@mindsp ring.com> wrote...[color=blue]
                              > Jack Klein wrote:[color=green]
                              > >
                              > > I should have elaborated. {0} works in all cases to initialize any
                              > > static or automatic scalar or aggregate type.[/color]
                              >
                              > What you said, could also be used to explain a preference for
                              > char *pointer = 0;
                              > over
                              > char *pointer = NULL;[/color]

                              Not quite. Since a void* is implicitly converted to any type*, the two
                              examples are exactly equivalent regardless of any of the two standard
                              definitions of NULL.
                              [color=blue]
                              > Is that your preference also ?[/color]

                              My preference is the latter, but it is only a style issue, nothing else.



                              Comment

                              • Peter Pichler

                                #30
                                Re: NULL pointer and zero value

                                "pete" <pfiland@mindsp ring.com> wrote...[color=blue]
                                > Peter Pichler wrote:[color=green]
                                > > "pete" <pfiland@mindsp ring.com> wrote...[color=darkred]
                                > > > Jack Klein wrote:
                                > > >
                                > > > What you said, could also be used to explain a preference for
                                > > > char *pointer = 0;
                                > > > over
                                > > > char *pointer = NULL;[/color]
                                > >
                                > > Not quite.
                                > > Since a void* is implicitly converted to any type*, the two
                                > > examples are exactly equivalent regardless of any of the two standard
                                > > definitions of NULL.[/color]
                                >
                                > Exactly quite.
                                > ModuleFunction garMF[N] = {NULL};
                                > and
                                > ModuleFunction garMF[N] = {0};
                                > are exactly equivalent regardless of any of the two standard
                                > definitions of NULL.[/color]

                                Only if garMF[N] is a pointer.

                                If I remember correctly, the thread started with explicit initialization
                                of struct members with memset, {NULL} or {0}. Of those three, memset can
                                only be portably used if the struct is a wrapper for an usigned char array,
                                anything else may lead to a UB. {NULL} can only be used if the first member
                                of such struct is a pointer. {0}, however, can be used in any context.
                                [color=blue][color=green][color=darkred]
                                > > > Is that your preference also ?[/color]
                                > >
                                > > My preference is the latter,
                                > > but it is only a style issue, nothing else.[/color]
                                >
                                > Style issues are on topic.
                                > Reasons for prefering one style over another
                                > which involve legibility and maintainability ,
                                > are the more interesting ones.[/color]

                                I didn't say it was not on-topic. Whether or not it is a /good/ topic
                                is quite a different question though ;-)

                                Peter


                                Comment

                                Working...