how to reset all structure fields to 0 or NULL

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

    how to reset all structure fields to 0 or NULL

    Hello, All!

    I have a structure:

    typedef struct cmd_line_s {
    char *cl_action;
    char *cl_args[10];
    unsigned char cl_args_num;
    void *cl_handler;
    } cmd_line_t;

    At initialization time I'm dooing cmd_line_t cli = { NULL, NULL, 0, NULL };

    After dealing with 'cli' I'd like to reset fields values to { NULL, NULL, 0,
    NULL } state again, what is the most correct and easy way?

    Thank you!

    With best regards, Roman Mashak. E-mail: mrv@tusur.ru


  • Eric Sosman

    #2
    Re: how to reset all structure fields to 0 or NULL

    Roman Mashak wrote:[color=blue]
    > Hello, All!
    >
    > I have a structure:
    >
    > typedef struct cmd_line_s {
    > char *cl_action;
    > char *cl_args[10];
    > unsigned char cl_args_num;
    > void *cl_handler;
    > } cmd_line_t;
    >
    > At initialization time I'm dooing cmd_line_t cli = { NULL, NULL, 0, NULL };
    >
    > After dealing with 'cli' I'd like to reset fields values to { NULL, NULL, 0,
    > NULL } state again, what is the most correct and easy way?[/color]

    One way is to copy from an already-initialized instance:

    const cmd_line_t empty_line = { NULL, NULL, 0, NULL };
    cmd_line_t cli = { NULL, NULL, 0, NULL };
    ...
    /* do things with cli */
    ...
    cli = empty_line;
    /* cli is now re-initialized */

    C99 provides another way to do this, but it's not all that much
    more convenient than the above in this sort of usage.

    The initializer you've shown isn't quite complete, and may
    cause some compilers to emit warning messages. You might try
    rewriting it as

    { NULL, { NULL }, 0, NULL }

    or even as

    { NULL,
    { NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL } ,
    0,
    NULL }

    or perhaps just as

    { 0 }

    --
    Eric Sosman
    esosman@acm-dot-org.invalid

    Comment

    • Walter Roberson

      #3
      Re: how to reset all structure fields to 0 or NULL

      In article <d8lenp$lb7$1@r elay.tomsk.ru>, Roman Mashak <mrv@tusur.ru > wrote:[color=blue]
      >I have a structure:[/color]
      [color=blue]
      > unsigned char cl_args_num;
      > void *cl_handler;[/color]
      [color=blue]
      >After dealing with 'cli' I'd like to reset fields values to { NULL, NULL, 0,
      >NULL } state again, what is the most correct and easy way?[/color]

      Keep a null copy around and do a structure assignment. After, that
      is, having freed whatever dynamic memory is appropriate.

      --
      Entropy is the logarithm of probability -- Boltzmann

      Comment

      • Roman Mashak

        #4
        Re: how to reset all structure fields to 0 or NULL

        Hello, Eric!
        You wrote on Mon, 13 Jun 2005 22:45:55 -0400:

        [skip]
        ES> The initializer you've shown isn't quite complete, and may
        ES> cause some compilers to emit warning messages. You might try
        ES> rewriting it as

        ES> { NULL, { NULL }, 0, NULL }

        ES> or even as

        ES> { NULL,
        ES> { NULL, NULL, NULL, NULL, NULL,
        ES> NULL, NULL, NULL, NULL, NULL } ,
        ES> 0,
        ES> NULL }

        Thanks a lot!

        With best regards, Roman Mashak. E-mail: mrv@tusur.ru


        Comment

        • Jean-Claude Arbaut

          #5
          Re : how to reset all structure fields to 0 or NULL




          Le 14/06/2005 04:17, dans d8lenp$lb7$1@re lay.tomsk.ru, « Roman Mashak »
          <mrv@tusur.ru > a écrit :
          [color=blue]
          > Hello, All!
          >
          > I have a structure:
          >
          > typedef struct cmd_line_s {
          > char *cl_action;
          > char *cl_args[10];
          > unsigned char cl_args_num;
          > void *cl_handler;
          > } cmd_line_t;
          >
          > At initialization time I'm dooing cmd_line_t cli = { NULL, NULL, 0, NULL };
          >
          > After dealing with 'cli' I'd like to reset fields values to { NULL, NULL, 0,
          > NULL } state again, what is the most correct and easy way?
          >
          > Thank you!
          >
          > With best regards, Roman Mashak. E-mail: mrv@tusur.ru
          >
          >[/color]

          Most correct I don't know, but "memset" should work...

          Comment

          • pete

            #6
            Re: Re : how to reset all structure fields to 0 or NULL

            Jean-Claude Arbaut wrote:[color=blue]
            >
            > Le 14/06/2005 04:17, dans d8lenp$lb7$1@re lay.tomsk.ru, « Roman Mashak »
            > <mrv@tusur.ru > a écrit :
            >[color=green]
            > > Hello, All!
            > >
            > > I have a structure:
            > >
            > > typedef struct cmd_line_s {
            > > char *cl_action;
            > > char *cl_args[10];
            > > unsigned char cl_args_num;
            > > void *cl_handler;
            > > } cmd_line_t;
            > >
            > > At initialization time I'm dooing cmd_line_t
            > > cli = { NULL, NULL, 0, NULL };
            > >
            > > After dealing with 'cli' I'd like to reset
            > > fields values to { NULL, NULL, 0,
            > > NULL } state again, what is the most correct and easy way?
            > >
            > > Thank you!
            > >
            > > With best regards, Roman Mashak. E-mail: mrv@tusur.ru
            > >
            > >[/color]
            >
            > Most correct I don't know, but "memset" should work...[/color]

            memset(&pointer s, 0, sizeof pointers)
            is the wrong way to set pointers to NULL.

            --
            pete

            Comment

            • Jean-Claude Arbaut

              #7
              Re : Re : how to reset all structure fields to 0 or NULL

              [color=blue]
              > memset(&pointer s, 0, sizeof pointers)
              > is the wrong way to set pointers to NULL.[/color]

              Oh, excuse me, I thought NULL was null :-)

              Comment

              • Dik T. Winter

                #8
                Re: Re : Re : how to reset all structure fields to 0 or NULL

                In article <BED491DD.3131% jean-claude.arbaut@l aposte.net> Jean-Claude Arbaut <jean-claude.arbaut@l aposte.net> writes:[color=blue]
                >[color=green]
                > > memset(&pointer s, 0, sizeof pointers)
                > > is the wrong way to set pointers to NULL.[/color]
                >
                > Oh, excuse me, I thought NULL was null :-)[/color]

                The null pointer does not have necessarily all bits 0 (as a floating
                point 0.0 has not necessarily all bits 0).
                --
                dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                Comment

                • Jean-Claude Arbaut

                  #9
                  Re : Re : Re : how to reset all structure fields to 0 or NULL




                  Le 14/06/2005 14:21, dans II2qB6.7tC@cwi. nl, « Dik T. Winter »
                  <Dik.Winter@cwi .nl> a écrit :
                  [color=blue]
                  > In article <BED491DD.3131% jean-claude.arbaut@l aposte.net> Jean-Claude Arbaut
                  > <jean-claude.arbaut@l aposte.net> writes:[color=green]
                  >>[color=darkred]
                  >>> memset(&pointer s, 0, sizeof pointers)
                  >>> is the wrong way to set pointers to NULL.[/color]
                  >>
                  >> Oh, excuse me, I thought NULL was null :-)[/color]
                  >
                  > The null pointer does not have necessarily all bits 0 (as a floating
                  > point 0.0 has not necessarily all bits 0).[/color]

                  Ok. I don't know all architectures, so assigning 0 may be non portable,
                  although on some OS (MacOSX for example), NULL is defigned as 0:

                  #define NULL __DARWIN_NULL
                  #define __DARWIN_NULL ((void *)0)

                  And the example of floating point 0.0 is irrelevant, +0.0 and -0.0
                  are two distinct values.

                  Just one question unrelated with NULLs, is it allowed to write "sizeof int",
                  or only "sizeof(int )" ? I wonder if GCC correctly follows the Standard...
                  I get:

                  test.c: In function 'main':
                  test.c:5: error: parse error before 'int'

                  Comment

                  • Dik T. Winter

                    #10
                    Re: Re : Re : Re : how to reset all structure fields to 0 or NULL

                    In article <BED4A06D.31DD% jean-claude.arbaut@l aposte.net> Jean-Claude Arbaut <jean-claude.arbaut@l aposte.net> writes:[color=blue]
                    > Le 14/06/2005 14:21, dans II2qB6.7tC@cwi. nl, « Dik T. Winter »
                    > <Dik.Winter@cwi .nl> a écrit :[color=green]
                    > > In article <BED491DD.3131% jean-claude.arbaut@l aposte.net> Jean-Claude Arbaut
                    > > <jean-claude.arbaut@l aposte.net> writes:[/color][/color]
                    ....[color=blue][color=green][color=darkred]
                    > >> Oh, excuse me, I thought NULL was null :-)[/color]
                    > >
                    > > The null pointer does not have necessarily all bits 0 (as a floating
                    > > point 0.0 has not necessarily all bits 0).[/color]
                    >
                    > Ok. I don't know all architectures, so assigning 0 may be non portable,
                    > although on some OS (MacOSX for example), NULL is defigned as 0:
                    >
                    > #define NULL __DARWIN_NULL
                    > #define __DARWIN_NULL ((void *)0)[/color]

                    Perfectly conforming to the standard. And if that is done on a machine
                    where the 0 pointer has not all bits 0, the compiler has to ensure that
                    the proper bit pattern is used for a null pointer, although the source
                    may contain the constant 0. 0 in a pointer context does *not* mean a
                    pointer with all bits 0, it means a null pointer.
                    [color=blue]
                    > And the example of floating point 0.0 is irrelevant, +0.0 and -0.0
                    > are two distinct values.[/color]

                    Not in C and not on all machines. But it is relevant. I know one
                    machine where all bits 0 is a non-normalized 0.0. So if a has that
                    value, and b is 3.5, a + b will be 3.0.
                    [color=blue]
                    > Just one question unrelated with NULLs, is it allowed to write "sizeof int",
                    > or only "sizeof(int )" ? I wonder if GCC correctly follows the Standard...
                    > I get:
                    >
                    > test.c: In function 'main':
                    > test.c:5: error: parse error before 'int'[/color]

                    Only sizeof(int), see the standard. The parenthesis are required when sizeof
                    is applied to a type.
                    --
                    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                    Comment

                    • Jean-Claude Arbaut

                      #11
                      Re : Re : Re : Re : how to reset all structure fields to 0 or NULL




                      Le 14/06/2005 15:54, dans II2unJ.8pF@cwi. nl, « Dik T. Winter »
                      <Dik.Winter@cwi .nl> a écrit :
                      [color=blue]
                      > In article <BED4A06D.31DD% jean-claude.arbaut@l aposte.net> Jean-Claude Arbaut
                      > <jean-claude.arbaut@l aposte.net> writes:[color=green]
                      >> Le 14/06/2005 14:21, dans II2qB6.7tC@cwi. nl, « Dik T. Winter »
                      >> <Dik.Winter@cwi .nl> a écrit :[color=darkred]
                      >>> In article <BED491DD.3131% jean-claude.arbaut@l aposte.net> Jean-Claude Arbaut
                      >>> <jean-claude.arbaut@l aposte.net> writes:[/color][/color]
                      > ...
                      >[color=green][color=darkred]
                      >>>> Oh, excuse me, I thought NULL was null :-)
                      >>>
                      >>> The null pointer does not have necessarily all bits 0 (as a floating
                      >>> point 0.0 has not necessarily all bits 0).[/color]
                      >>
                      >> Ok. I don't know all architectures, so assigning 0 may be non portable,
                      >> although on some OS (MacOSX for example), NULL is defigned as 0:
                      >>
                      >> #define NULL __DARWIN_NULL
                      >> #define __DARWIN_NULL ((void *)0)[/color]
                      >
                      > Perfectly conforming to the standard. And if that is done on a machine
                      > where the 0 pointer has not all bits 0, the compiler has to ensure that
                      > the proper bit pattern is used for a null pointer, although the source
                      > may contain the constant 0. 0 in a pointer context does *not* mean a
                      > pointer with all bits 0, it means a null pointer.[/color]

                      You're right. But on MacOSX/PowerPC, the NULL pointer *is* 0. Not compiler
                      trick, you can check the assembly output. And the "segment zero" is
                      here to crash if the NULL pointer is dereferenced.
                      [color=blue][color=green]
                      >> And the example of floating point 0.0 is irrelevant, +0.0 and -0.0
                      >> are two distinct values.[/color]
                      >
                      > Not in C and not on all machines. But it is relevant. I know one
                      > machine where all bits 0 is a non-normalized 0.0. So if a has that
                      > value, and b is 3.5, a + b will be 3.0.[/color]

                      Sorry, I had IEEE 754 in mind. 0.0 and -0.0 are distinct (juste compute
                      1/x to check this), but 0.0 == -0.0. I'm pretty sure it's perfect IEEE
                      behaviour. But you're right: one must not use memset to initialize
                      a zero fp vector... funny.
                      [color=blue][color=green]
                      >> Just one question unrelated with NULLs, is it allowed to write "sizeof int",
                      >> or only "sizeof(int )" ? I wonder if GCC correctly follows the Standard...
                      >> I get:
                      >>
                      >> test.c: In function 'main':
                      >> test.c:5: error: parse error before 'int'[/color]
                      >
                      > Only sizeof(int), see the standard. The parenthesis are required when sizeof
                      > is applied to a type.[/color]

                      Thanks.

                      Comment

                      • Clark S. Cox III

                        #12
                        Re: Re : Re : Re : how to reset all structure fields to 0 or NULL

                        On 2005-06-14 08:55:41 -0400, Jean-Claude Arbaut
                        <jean-claude.arbaut@l aposte.net> said:[color=blue]
                        >
                        > Le 14/06/2005 14:21, dans II2qB6.7tC@cwi. nl, « Dik T. Winter »
                        > <Dik.Winter@cwi .nl> a écrit :
                        >[color=green]
                        >> In article <BED491DD.3131% jean-claude.arbaut@l aposte.net> Jean-Claude Arbaut
                        >> <jean-claude.arbaut@l aposte.net> writes:[color=darkred]
                        >>>
                        >>>> memset(&pointer s, 0, sizeof pointers)
                        >>>> is the wrong way to set pointers to NULL.
                        >>>
                        >>> Oh, excuse me, I thought NULL was null :-)[/color]
                        >>
                        >> The null pointer does not have necessarily all bits 0 (as a floating
                        >> point 0.0 has not necessarily all bits 0).[/color]
                        >
                        > Ok. I don't know all architectures, so assigning 0 may be non portable,
                        > although on some OS (MacOSX for example),[/color]

                        Assigning zero to a pointer variable is *always* OK, and will always
                        result in a NULL pointer. But that doesn't mean that the actual, in
                        memory representation of that NULL pointer is "all-bits zero"
                        [color=blue]
                        > And the example of floating point 0.0 is irrelevant, +0.0 and -0.0
                        > are two distinct values.[/color]

                        Not always, and the example is quite relevant.
                        [color=blue]
                        >
                        > Just one question unrelated with NULLs, is it allowed to write "sizeof int",
                        > or only "sizeof(int )" ?[/color]

                        No, when applied to a type, only sizeof(type) is allowed.

                        --
                        Clark S. Cox, III
                        clarkcox3@gmail .com

                        Comment

                        • S.Tobias

                          #13
                          Re: [OT] how to reset all structure fields to 0 or NULL

                          Jean-Claude Arbaut <jean-claude.arbaut@l aposte.net> wrote:
                          [snip all]

                          Could you please fix something in your reader, so that it doesn't
                          insert a space between "Re" and ":" in the subject? I think other
                          readers don't recognize "Re :" as "regarding" (or "reply"?) prefix
                          and add their own "Re:"; then probably your reader changes it to "Re :"
                          again, and the "game of life" restarts. In my reader in threads
                          menu I'll soon be seeing only "Re : Re : Re : Re : Re : Re : ...".

                          --
                          Stan Tobias
                          mailx `echo siXtY@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

                          Comment

                          • Jean-Claude Arbaut

                            #14
                            Re: [OT] how to reset all structure fields to 0 or NULL




                            Le 14/06/2005 17:11, dans 3h8adcFfh61qU1@ individual.net, « S.Tobias »
                            <siXtY@FamOuS.B edBuG.pAlS.INVA LID> a écrit :
                            [color=blue]
                            > Jean-Claude Arbaut <jean-claude.arbaut@l aposte.net> wrote:
                            > [snip all]
                            >
                            > Could you please fix something in your reader, so that it doesn't
                            > insert a space between "Re" and ":" in the subject? I think other
                            > readers don't recognize "Re :" as "regarding" (or "reply"?) prefix
                            > and add their own "Re:"; then probably your reader changes it to "Re :"
                            > again, and the "game of life" restarts. In my reader in threads
                            > menu I'll soon be seeing only "Re : Re : Re : Re : Re : Re : ...".[/color]

                            Sorry, I didn't see... The origin is Microsoft Entourage and its
                            interpretation of french language rule: it puts a space before a
                            colon, by default. I'll probably switch my news reader soon :-)

                            Comment

                            • CBFalconer

                              #15
                              Re: Re : how to reset all structure fields to 0 or NULL

                              Jean-Claude Arbaut wrote:[color=blue]
                              > « Roman Mashak » <mrv@tusur.ru > a écrit :
                              >[color=green]
                              >> typedef struct cmd_line_s {
                              >> char *cl_action;
                              >> char *cl_args[10];
                              >> unsigned char cl_args_num;
                              >> void *cl_handler;
                              >> } cmd_line_t;
                              >>
                              >> At initialization time I'm dooing
                              >> cmd_line_t cli = { NULL, NULL, 0, NULL };
                              >>
                              >> After dealing with 'cli' I'd like to reset fields values to
                              >> { NULL, NULL, 0, NULL } state again, what is the most correct
                              >> and easy way?[/color]
                              >
                              > Most correct I don't know, but "memset" should work...[/color]

                              Definitely incorrect. Those may not be NULLs. Suggested means:

                              ....
                              /* Very suitable use for a global */
                              const cmd_line_t clempty = {NULL, NULL, 0, NULL);
                              ....
                              int main(...)
                              {
                              ....
                              cmd_line_t cli = clempty;
                              ....
                              do {
                              /* fool with cli */
                              cli = clempty; /* rezero it */
                              } while (something);
                              ....
                              }

                              --
                              "If you want to post a followup via groups.google.c om, don't use
                              the broken "Reply" link at the bottom of the article. Click on
                              "show options" at the top of the article, then click on the
                              "Reply" at the bottom of the article headers." - Keith Thompson

                              Comment

                              Working...