memset

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

    memset

    Hi,
    Let say I have:
    char szBuffer[20]="";

    1. Which one is the better option to reset the buffer ? and why?
    strcpy(szBuffer ,"");
    memset(szBuffer ,'\0',sizeof(sz Buffer));

    OR

    *szBuffer=0;


    2. Can I use just memset(...) ONLY ? Instead of strcpy(...) memset(...) ?

    Thanks


  • Wade Yin

    #2
    Re: memset

    "Magix" <magix@asia.com > writes:
    [color=blue]
    > Let say I have:
    > char szBuffer[20]="";
    >
    > 1. Which one is the better option to reset the buffer ? and why?
    > strcpy(szBuffer ,"");
    > memset(szBuffer ,'\0',sizeof(sz Buffer));
    >
    > OR
    >
    > *szBuffer=0;
    >
    >
    > 2. Can I use just memset(...) ONLY ? Instead of strcpy(...) memset(...) ?
    >[/color]

    if you wanna set szBuffer to zero,
    memset(szBuffer , 0, 20); is OK...

    Why use strcpy?



    Comment

    • Arthur J. O'Dwyer

      #3
      Re: memset


      On Wed, 25 Aug 2004, Magix wrote:[color=blue]
      >
      > Hi,
      > Let say I have:
      > char szBuffer[20]="";
      >
      > 1. Which one is the better option to reset the buffer ? and why?[/color]

      Define "reset the buffer." If you mean, "put an empty string in
      the buffer," then you could write

      strcpy(szBuffer , "");

      which (assuming you've #included <string.h>) is exactly equivalent to

      szBuffer[0] = '\0';

      If on the other hand you mean "set each byte of the buffer to
      zero," then you ought to use

      memset(szBuffer , 0, sizeof szBuffer);

      [color=blue]
      > strcpy(szBuffer ,"");[/color]

      This does the former.
      [color=blue]
      > memset(szBuffer ,'\0',sizeof(sz Buffer));[/color]

      This does the latter.
      [color=blue]
      > OR
      >
      > *szBuffer=0;[/color]

      This does the former again.

      Setting each byte of the buffer to zero /will/ put an empty string
      in the buffer, but it will do more than that. If you want the more
      done, then do it. Otherwise, don't.

      -Arthur

      Comment

      • Kelsey Bjarnason

        #4
        Re: memset

        On Wed, 25 Aug 2004 10:09:31 +0800, Magix wrote:
        [color=blue]
        > Hi,
        > Let say I have:
        > char szBuffer[20]="";
        >
        > 1. Which one is the better option to reset the buffer ? and why?
        > strcpy(szBuffer ,"");
        > memset(szBuffer ,'\0',sizeof(sz Buffer));
        >
        > OR
        >
        > *szBuffer=0;
        >
        >
        > 2. Can I use just memset(...) ONLY ? Instead of strcpy(...) memset(...) ?
        >
        > Thanks[/color]

        Personally, I'd probably not use any of them. Why? Consider: why are you
        creating a buffer in the first place? Presumably to store data into it -
        which probably means something _other than_ zeroes. So why try to stuff
        zeroes into it to start with? It's a purely redundant operation, except in
        the case of a char buffer which will subsequently have data appended by
        strcat. It's a waste of cycles otherwise.

        If you really do need to do this, though, the answer to your question
        depends on _why_ you need to do it. If you're going to strcat things, then
        the simple *szBuffer = 0; is sufficient (and functionally identical to the
        strcpy).

        Comment

        • Magix

          #5
          Re: memset


          "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> wrote in message
          news:Pine.LNX.4 .60-041.04082423042 30.7909@unix44. andrew.cmu.edu. ..[color=blue]
          >
          > On Wed, 25 Aug 2004, Magix wrote:[color=green]
          > >
          > > Hi,
          > > Let say I have:
          > > char szBuffer[20]="";
          > >
          > > 1. Which one is the better option to reset the buffer ? and why?[/color]
          >
          > Define "reset the buffer." If you mean, "put an empty string in
          > the buffer," then you could write
          >[/color]

          "Reset the buffer" means I want to reuse the buffer for others and to clean
          any previous info that in the buffer.
          Example: suppose if szBuffer has "ABCDEFGHIJKLMN "

          After some process, I set szBuffer has "123456", probably use strcat or
          something like that

          When come to display the whole szBuffer,
          the output of whole szBuffer should be 123456 only, not 123456GHIJKLMN

          That's why I posted this questions. If I used *szBuffer=0 only, then I used
          strcat, szBuffer will still has the previous info.


          [color=blue]
          > strcpy(szBuffer , "");
          >
          > which (assuming you've #included <string.h>) is exactly equivalent to
          >
          > szBuffer[0] = '\0';
          >
          > If on the other hand you mean "set each byte of the buffer to
          > zero," then you ought to use
          >
          > memset(szBuffer , 0, sizeof szBuffer);
          >
          >[color=green]
          > > strcpy(szBuffer ,"");[/color]
          >
          > This does the former.
          >[color=green]
          > > memset(szBuffer ,'\0',sizeof(sz Buffer));[/color]
          >
          > This does the latter.
          >[color=green]
          > > OR
          > >
          > > *szBuffer=0;[/color]
          >
          > This does the former again.
          >
          > Setting each byte of the buffer to zero /will/ put an empty string
          > in the buffer, but it will do more than that. If you want the more
          > done, then do it. Otherwise, don't.
          >
          > -Arthur
          >[/color]


          Comment

          • Dan Pop

            #6
            Re: memset

            In <Pine.LNX.4.6 0-041.04082423042 30.7909@unix44. andrew.cmu.edu> "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> writes:

            [color=blue]
            >which (assuming you've #included <string.h>) is exactly equivalent to
            >
            > szBuffer[0] = '\0';
            >
            > If on the other hand you mean "set each byte of the buffer to
            >zero," then you ought to use
            >
            > memset(szBuffer , 0, sizeof szBuffer);[/color]

            Why '\0' in one case and 0 in the other?

            The only reason to bother typing '\0' is style, but then both instances
            require '\0'...

            Dan
            --
            Dan Pop
            DESY Zeuthen, RZ group
            Email: Dan.Pop@ifh.de

            Comment

            • Dan Pop

              #7
              Re: memset

              In <412c3ce4$1_1@n ews.tm.net.my> "Magix" <magix@asia.com > writes:
              [color=blue]
              >"Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> wrote in message
              >news:Pine.LNX. 4.60-041.04082423042 30.7909@unix44. andrew.cmu.edu. ..[color=green]
              >>
              >> On Wed, 25 Aug 2004, Magix wrote:[color=darkred]
              >> >
              >> > Hi,
              >> > Let say I have:
              >> > char szBuffer[20]="";
              >> >
              >> > 1. Which one is the better option to reset the buffer ? and why?[/color]
              >>
              >> Define "reset the buffer." If you mean, "put an empty string in
              >> the buffer," then you could write
              >>[/color]
              >
              >"Reset the buffer" means I want to reuse the buffer for others and to clean
              >any previous info that in the buffer.
              >Example: suppose if szBuffer has "ABCDEFGHIJKLMN "
              >
              >After some process, I set szBuffer has "123456", probably use strcat or
              >something like that
              >
              >When come to display the whole szBuffer,
              >the output of whole szBuffer should be 123456 only, not 123456GHIJKLMN
              >
              >That's why I posted this questions. If I used *szBuffer=0 only, then I used
              >strcat, szBuffer will still has the previous info.[/color]

              Do you understand what a string is?

              The contents of the whole buffer will be "123456\0HIJKLM N", which, when
              interpreted as a string, is "123456", anything following the terminating
              null character being ignored.

              So, when dealing with buffers that are supposed to contain C strings,
              clearing the first byte of the buffer is enough. If the buffer is
              used for other purposes, the right way of clearing it is dictated by
              the exact purpose. memset() always works, but it is seldom necessary.

              A typical case where you may want to use memset() even for strings is
              when exporting security sensitive data from your program. The trailing
              bytes in the buffer may contain confidential information. The alternative
              is to use strncpy, that clears everything after the last character of the
              destination string, but strncpy has its own gotchas and works best only
              in expert hands.

              Dan
              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              • Arthur J. O'Dwyer

                #8
                Re: memset


                On Wed, 25 Aug 2004, Dan Pop wrote:[color=blue]
                >
                > "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> writes:[color=green]
                >>
                >> szBuffer[0] = '\0';
                >>
                >> If on the other hand you mean "set each byte of the buffer to
                >> zero," then you ought to use
                >>
                >> memset(szBuffer , 0, sizeof szBuffer);[/color]
                >
                > Why '\0' in one case and 0 in the other?
                >
                > The only reason to bother typing '\0' is style, but then both instances
                > require '\0'...[/color]

                I consider '\0' to indicate "character zero-byte," whereas 'memset'
                conceptually takes a "byte zero-byte" (for which I'd use an 'unsigned
                char'); hence the unadorned 0.
                Maybe if the OP were wanting to store a compressed list of strings
                in 'szBuffer', I'd write

                memset(szBuffer , '\0', sizeof szBuffer); /* 101 empty strings */
                memset(szBuffer , 'A', sizeof szBuffer); /* 1 string of 100 'A's */

                but that wasn't the connotation I was meaning to convey. For just
                setting each byte (or bit) of the object to zero, I use

                memset(szBuffer , 0, sizeof szBuffer); /* byte zero, not "char" zero */

                YMMV.
                -Arthur

                Comment

                • Dan Pop

                  #9
                  Re: memset

                  In <Pine.LNX.4.6 0-041.04082513431 10.4437@unix47. andrew.cmu.edu> "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> writes:

                  [color=blue]
                  >On Wed, 25 Aug 2004, Dan Pop wrote:[color=green]
                  >>
                  >> "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> writes:[color=darkred]
                  >>>
                  >>> szBuffer[0] = '\0';
                  >>>
                  >>> If on the other hand you mean "set each byte of the buffer to
                  >>> zero," then you ought to use
                  >>>
                  >>> memset(szBuffer , 0, sizeof szBuffer);[/color]
                  >>
                  >> Why '\0' in one case and 0 in the other?
                  >>
                  >> The only reason to bother typing '\0' is style, but then both instances
                  >> require '\0'...[/color]
                  >
                  > I consider '\0' to indicate "character zero-byte," whereas 'memset'
                  >conceptually takes a "byte zero-byte" (for which I'd use an 'unsigned
                  >char'); hence the unadorned 0.[/color]

                  Last time I've checked, "byte" and "character" were the same thing in C
                  and the intent of the memset call was to fill the character buffer with
                  null characters.

                  You'd have a point if the array didn't have character type and/or wasn't
                  used for storing character data:

                  int array[SIZE];
                  ...
                  memset(array, 0, sizeof array);

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de

                  Comment

                  • Malcolm

                    #10
                    Re: memset


                    "Magix" <magix@asia.com > wrote[color=blue]
                    >
                    > char szBuffer[20]="";
                    >
                    > 1. Which one is the better option to reset the buffer ? and why?
                    > strcpy(szBuffer ,"");
                    > memset(szBuffer ,'\0',sizeof(sz Buffer));
                    >
                    > OR
                    >
                    > *szBuffer=0;
                    >
                    >
                    > 2. Can I use just memset(...) ONLY ? Instead of strcpy(...) memset(...) ?
                    >[/color]
                    Usually it is sufficient to set only the first character of an empty string
                    to zero. You only need to memset() the whole buffer if you want to destroy
                    all traces of the old string, maybe for security or debugging purposes.

                    strcpy(szBuffer , "") or *szBuffer = 0 will both do this, but strcpy() will
                    usually incur the overhead of a function call.

                    However it is not likely that this will be in a sensitive place, so won't
                    make a noticeable difference to running time. The reason for avoiding
                    strcpy() is that it is a gratuitous waste of cycles and a maintaining
                    programmer may wonder why you have used it. The reason for using strcpy() is
                    that it is clearer that your intention is to set szBuffer to the empty
                    string. So it depends whether your maintaining programmer is an old school
                    hacker or a more modern "literate programming type". There isn't a good
                    answer, but the point is that readability is the overwhelming consideration.
                    You need to save a lot of machine cycles to pay for ten seconds of a
                    programmer's time.


                    Comment

                    Working...