memset

Discussion in 'C Programming' started by Magix, Aug 25, 2004.

  1. Magix

    Magix Guest

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

    OR

    *szBuffer=0;


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

    Thanks
    Magix, Aug 25, 2004
    #1
    1. Advertising

  2. Magix

    Wade Yin Guest

    "Magix" <> writes:

    > 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(szBuffer));
    >
    > OR
    >
    > *szBuffer=0;
    >
    >
    > 2. Can I use just memset(...) ONLY ? Instead of strcpy(...) memset(...) ?
    >


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

    Why use strcpy?
    Wade Yin, Aug 25, 2004
    #2
    1. Advertising

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


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


    > strcpy(szBuffer,"");


    This does the former.

    > memset(szBuffer,'\0',sizeof(szBuffer));


    This does the latter.

    > OR
    >
    > *szBuffer=0;


    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
    Arthur J. O'Dwyer, Aug 25, 2004
    #3
  4. On Wed, 25 Aug 2004 10:09:31 +0800, Magix wrote:

    > 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(szBuffer));
    >
    > OR
    >
    > *szBuffer=0;
    >
    >
    > 2. Can I use just memset(...) ONLY ? Instead of strcpy(...) memset(...) ?
    >
    > Thanks


    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).
    Kelsey Bjarnason, Aug 25, 2004
    #4
  5. Magix

    Magix Guest

    "Arthur J. O'Dwyer" <> wrote in message
    news:p...
    >
    > On Wed, 25 Aug 2004, Magix wrote:
    > >
    > > Hi,
    > > Let say I have:
    > > char szBuffer[20]="";
    > >
    > > 1. Which one is the better option to reset the buffer ? and why?

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


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



    > 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);
    >
    >
    > > strcpy(szBuffer,"");

    >
    > This does the former.
    >
    > > memset(szBuffer,'\0',sizeof(szBuffer));

    >
    > This does the latter.
    >
    > > OR
    > >
    > > *szBuffer=0;

    >
    > 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
    >
    Magix, Aug 25, 2004
    #5
  6. Magix

    Dan Pop Guest

    In <> "Arthur J. O'Dwyer" <> writes:


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


    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, Aug 25, 2004
    #6
  7. Magix

    Dan Pop Guest

    In <412c3ce4$> "Magix" <> writes:

    >"Arthur J. O'Dwyer" <> wrote in message
    >news:p...
    >>
    >> On Wed, 25 Aug 2004, Magix wrote:
    >> >
    >> > Hi,
    >> > Let say I have:
    >> > char szBuffer[20]="";
    >> >
    >> > 1. Which one is the better option to reset the buffer ? and why?

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

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


    Do you understand what a string is?

    The contents of the whole buffer will be "123456\0HIJKLMN", 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, Aug 25, 2004
    #7
  8. On Wed, 25 Aug 2004, Dan Pop wrote:
    >
    > "Arthur J. O'Dwyer" <> writes:
    >>
    >> 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);

    >
    > 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'...


    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
    Arthur J. O'Dwyer, Aug 25, 2004
    #8
  9. Magix

    Dan Pop Guest

    In <> "Arthur J. O'Dwyer" <> writes:


    >On Wed, 25 Aug 2004, Dan Pop wrote:
    >>
    >> "Arthur J. O'Dwyer" <> writes:
    >>>
    >>> 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);

    >>
    >> 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'...

    >
    > 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.


    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, Aug 25, 2004
    #9
  10. Magix

    Malcolm Guest

    "Magix" <> wrote
    >
    > char szBuffer[20]="";
    >
    > 1. Which one is the better option to reset the buffer ? and why?
    > strcpy(szBuffer,"");
    > memset(szBuffer,'\0',sizeof(szBuffer));
    >
    > OR
    >
    > *szBuffer=0;
    >
    >
    > 2. Can I use just memset(...) ONLY ? Instead of strcpy(...) memset(...) ?
    >

    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.
    Malcolm, Aug 27, 2004
    #10
    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. k-man
    Replies:
    4
    Views:
    4,505
    Shelley Hebert
    Dec 18, 2003
  2. Joe C
    Replies:
    5
    Views:
    8,830
    tom_usenet
    Aug 24, 2004
  3. Replies:
    17
    Views:
    6,461
    Greg Comeau
    Sep 22, 2004
  4. Guest
    Replies:
    0
    Views:
    385
    Guest
    Sep 23, 2004
  5. Guest
    Replies:
    1
    Views:
    418
    Ron Natalie
    Sep 23, 2004
Loading...

Share This Page