memset

M

Magix

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
 
W

Wade Yin

Magix said:
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?
 
A

Arthur J. O'Dwyer

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
 
K

Kelsey Bjarnason

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

Magix

Arthur J. O'Dwyer said:
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
 
D

Dan Pop

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
 
D

Dan Pop

In said:
Arthur J. O'Dwyer said:
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
 
A

Arthur J. O'Dwyer

Arthur J. O'Dwyer said:
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
 
D

Dan Pop

Arthur J. O'Dwyer said:
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
 
M

Malcolm

Magix said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top