Salt_Peter
12/16/2008 9:54:00 AM
On Dec 16, 12:58 am, Jaydeep Chovatia <chovatia.jayd...@gmail.com>
wrote:
> Hi,
>
> So many times i have used memset to set character string to NULL
> initially. But i would like to know the reason why/when/where it
> should be used.
C++ typically doesn't suffer from those problems.
A std::string is originally empty (hence nothing to memset), it's
dynamic and not null-terminated, yet it can spit out null-terminated
data when and if needed.
>
> I know that when we perform some operations like assign data to
> character, copy then '\0' gets appended to the string then why we
> should use memset?
>
> Thank You,
> Jaydeep
Using primitive arrays as buffers is the old way. If that terminator
gets omitted you get the infamous buffer overruns. Assign a data block
larger than the receiving buffer and you are doomed. At the very
least, primitive buffers should check that the incoming source is not
larger than the target buffer. Fat chance that will happen.
Better to use std::string, or std:vector< char >, etc.