Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > memset

Reply
Thread Tools

memset

 
 
Magix
Guest
Posts: n/a
 
      08-25-2004
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


 
Reply With Quote
 
 
 
 
Wade Yin
Guest
Posts: n/a
 
      08-25-2004
"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?



 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      08-25-2004

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

 
Reply With Quote
 
Kelsey Bjarnason
Guest
Posts: n/a
 
      08-25-2004
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).
 
Reply With Quote
 
Magix
Guest
Posts: n/a
 
      08-25-2004

"Arthur J. O'Dwyer" <> wrote in message
newsine.LNX.4.60-...
>
> 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
>



 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-25-2004
In <Pine.LNX.4.60-> "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:
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-25-2004
In <412c3ce4$> "Magix" <> writes:

>"Arthur J. O'Dwyer" <> wrote in message
>newsine.LNX.4.60-...
>>
>> 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:
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      08-25-2004

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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-25-2004
In <Pine.LNX.4.60-> "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:
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      08-27-2004

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: "memset" vs "= {0}"...Are they equivalent if your initializing variables? C++ 1 09-23-2004 02:03 PM
Re: "memset" vs "= {0}"...Are they equivalent if your initializing variables? C++ 0 09-23-2004 01:28 PM
"memset" vs "= {0}"...Are they equivalent if your initializing variables? Nollie@runtime.com C++ 17 09-22-2004 06:06 PM
memset vs fill and iterators vs pointers Joe C C++ 5 08-24-2004 11:51 AM
2 questions: speed of memset() and pointer to multi-arrays k-man C++ 4 12-18-2003 08:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57