In <412c3ce4$> "Magix" <> writes:
>"Arthur J. O'Dwyer" <> wrote in message
>news
ine.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: