NULL pointer and zero value

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

    NULL pointer and zero value

    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;"

    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 );


    Thanks for your help,

    DT
  • Richard Heathfield

    #2
    Re: NULL pointer and zero value

    vp wrote:
    [color=blue]
    > 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. NULL is not necessarily all-bits-zero.

    Your technique will, however, work by chance on quite a few platforms,
    because quite a few platforms do use all-bits-zero for NULL. If you like
    programming-by-casino, go ahead. :-)

    <snip>

    --
    Richard Heathfield : binary@eton.pow ernet.co.uk
    "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

    Comment

    • Christopher Benson-Manica

      #3
      Re: NULL pointer and zero value

      Richard Heathfield <invalid@addres s.co.uk.invalid > spoke thus:
      [color=blue][color=green]
      >> (all-bits 0 as NULL post)[/color]
      > If you like programming-by-casino, go ahead. :-)[/color]

      So the house wins on NULL and double NULL? ;)

      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • Martin Dickopp

        #4
        Re: NULL pointer and zero value

        dt1649651@yahoo .com (vp) writes:
        [color=blue]
        > 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=blue]
        > 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]

        If `garMF' has static storage duration, it is automatically initialized to
        zero if you don't initialize it explicitly. "Initialize d to zero" means
        that each non-compound object is initialized as if you assigned `0' to it
        (i.e. pointers are initialized to null pointers). For compound objects,
        its members or elements are recursively initialized in this way.

        Otherwise (if `garMF' doesn't have static storage duration) you'll have
        to loop.

        Martin

        Comment

        • Mark A. Odell

          #5
          Re: NULL pointer and zero value

          Richard Heathfield <invalid@addres s.co.uk.invalid > wrote in
          news:3ff17d2f@n ews2.power.net. uk:
          [color=blue]
          > No. NULL is not necessarily all-bits-zero.
          >
          > Your technique will, however, work by chance on quite a few platforms,
          > because quite a few platforms do use all-bits-zero for NULL. If you like
          > programming-by-casino, go ahead. :-)[/color]

          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;
          }

          --
          - Mark ->
          --

          Comment

          • pete

            #6
            Re: NULL pointer and zero value

            Martin Dickopp wrote:[color=blue]
            >
            > dt1649651@yahoo .com (vp) writes:
            >[color=green]
            > > 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=green]
            > > 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=blue]
            > 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};

            --
            pete

            Comment

            • pete

              #7
              Re: NULL pointer and zero value

              Mark A. Odell wrote:[color=blue]
              >
              > Richard Heathfield <invalid@addres s.co.uk.invalid > wrote in
              > news:3ff17d2f@n ews2.power.net. uk:
              >[color=green]
              > > No. NULL is not necessarily all-bits-zero.
              > >
              > > Your technique will, however,
              > > work by chance on quite a few platforms,
              > > because quite a few platforms do use all-bits-zero for NULL.
              > > If you like programming-by-casino, go ahead. :-)[/color]
              >
              > So are you saying that this isn't safe?
              >
              > #include <stdlib.h>
              >
              > int main(void)
              > {
              > int *pVar = malloc(1024);
              >
              > if (pVar) /* <--- Not safe? */[/color]

              Your code does not check to see if all bits are zero.

              Your question and corresponding code example
              are not relevant to what Richard Heathfield said.

              --
              pete

              Comment

              • Martin Dickopp

                #8
                Re: NULL pointer and zero value

                "Mark A. Odell" <nospam@embedde dfw.com> writes:
                [color=blue]
                > Richard Heathfield <invalid@addres s.co.uk.invalid > wrote in
                > news:3ff17d2f@n ews2.power.net. uk:
                >[color=green]
                > > No. NULL is not necessarily all-bits-zero.
                > >
                > > Your technique will, however, work by chance on quite a few platforms,
                > > because quite a few platforms do use all-bits-zero for NULL. If you like
                > > programming-by-casino, go ahead. :-)[/color]
                >
                > 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]

                The program is correct (i.e. strictly conforming). The `if' statement
                doesn't look at the representation (i.e. the bits) of `pVar', it simply
                compares `pVar' to `0'. A comparison between an expression of pointer
                type and `0', which is a null pointer constant in such a comparison, is
                valid.

                Martin

                Comment

                • pete

                  #9
                  Re: NULL pointer and zero value

                  pete wrote:[color=blue]
                  >
                  > Mark A. Odell wrote:[color=green]
                  > >
                  > > Richard Heathfield <invalid@addres s.co.uk.invalid > wrote in
                  > > news:3ff17d2f@n ews2.power.net. uk:
                  > >[color=darkred]
                  > > > No. NULL is not necessarily all-bits-zero.
                  > > >
                  > > > Your technique will, however,
                  > > > work by chance on quite a few platforms,
                  > > > because quite a few platforms do use all-bits-zero for NULL.
                  > > > If you like programming-by-casino, go ahead. :-)[/color]
                  > >
                  > > So are you saying that this isn't safe?
                  > >
                  > > #include <stdlib.h>
                  > >
                  > > int main(void)
                  > > {
                  > > int *pVar = malloc(1024);
                  > >
                  > > if (pVar) /* <--- Not safe? */[/color]
                  >
                  > Your code does not check to see if all bits are zero.
                  >
                  > Your question and corresponding code example
                  > are not relevant to what Richard Heathfield said.[/color]

                  The question is relevant, the answer is "no".

                  --
                  pete

                  Comment

                  • CBFalconer

                    #10
                    Re: NULL pointer and zero value

                    vp wrote:[color=blue]
                    >
                    > 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.

                    While it will often "work" on a particular system, it is not
                    portable and can lead to very mysterious bugs.

                    --
                    Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net> USE worldnet address!

                    Comment

                    • Mark A. Odell

                      #11
                      Re: NULL pointer and zero value

                      Martin Dickopp <expires-2004-01-31@zero-based.org> wrote in
                      news:bss11a$kme $02$2@news.t-online.com:
                      [color=blue][color=green]
                      >> 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]
                      >
                      > The program is correct (i.e. strictly conforming). The `if' statement
                      > doesn't look at the representation (i.e. the bits) of `pVar', it simply
                      > compares `pVar' to `0'. A comparison between an expression of pointer
                      > type and `0', which is a null pointer constant in such a comparison, is
                      > valid.[/color]

                      Excellent. Thank you.

                      --
                      - Mark ->
                      --

                      Comment

                      • vp

                        #12
                        Re: NULL pointer and zero value

                        dt1649651@yahoo .com (vp) wrote in message news:<244097ab. 0312300503.48de ff52@posting.go ogle.com>...[color=blue]
                        > 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]
                        [...]

                        Thanks for all of your replies and explanation. I see that I just can
                        not be lazy to make that memset. I don't want to put Las Vegas into my
                        app :-)

                        DT

                        Comment

                        • vp

                          #13
                          Re: NULL pointer and zero value

                          "Mark A. Odell" <nospam@embedde dfw.com> wrote in message news:<Xns94615B 16B259BCopyrigh tMarkOdell@130. 133.1.4>...[color=blue]
                          > Richard Heathfield <invalid@addres s.co.uk.invalid > wrote in
                          > news:3ff17d2f@n ews2.power.net. uk:
                          >[color=green]
                          > > No. NULL is not necessarily all-bits-zero.
                          > >
                          > > Your technique will, however, work by chance on quite a few platforms,
                          > > because quite a few platforms do use all-bits-zero for NULL. If you like
                          > > programming-by-casino, go ahead. :-)[/color]
                          >
                          > 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]

                          I guess the compiler will do the necessary comparison in this case. I
                          often see this style of code "if ( pointer )", but personally I often
                          write it like "if ( p != NULL )" just to remind me or my co-workers
                          that p is of pointer-type.

                          DT

                          Comment

                          • Mark A. Odell

                            #14
                            Re: NULL pointer and zero value

                            dt1649651@yahoo .com (vp) wrote in
                            news:244097ab.0 312301010.4362a 0db@posting.goo gle.com:
                            [color=blue][color=green]
                            >> if (pVar) /* <--- Not safe? */
                            >> {
                            >> free(pVar);
                            >> }
                            >>
                            >> return EXIT_SUCCESS;
                            >> }[/color]
                            >
                            > I guess the compiler will do the necessary comparison in this case. I
                            > often see this style of code "if ( pointer )", but personally I often
                            > write it like "if ( p != NULL )" just to remind me or my co-workers
                            > that p is of pointer-type.[/color]

                            I avoid explicit checks for boolean conditions and since all my pointer
                            vars have a lower-case p in front, e.g. pSomeName, this is never an issue
                            for me. Besides, if I want to know if 'foo' is a pointer then I just hover
                            over it with my mouse and CodeWright shows me the definition. I thought
                            all editors could do such simple things. ;-)

                            --
                            - Mark ->
                            --

                            Comment

                            • Keith Thompson

                              #15
                              Re: NULL pointer and zero value

                              dt1649651@yahoo .com (vp) writes:[color=blue]
                              > 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 C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>.
                              Read section 5, "Null Pointers".

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
                              Schroedinger does Shakespeare: "To be *and* not to be"
                              (Note new e-mail address)

                              Comment

                              Working...