Why memset should be used?

J

Jaydeep Chovatia

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.

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
 
I

Ian Collins

Jaydeep said:
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.
If you use std::string, never.
 
S

Salt_Peter

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

Guest

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.

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
 
G

Guest

So many times i have used memset to set character string to NULL
initially.

Did you mean NULL the null address pointer or nul the character
(usually written '\0'). Please don't put NULLs into a string
even just spent a couple of days removing stuff like that
to get rid of compiler warnings!

even in C I can't really see the point of something like:-

void foo()
{
char *s[42];
memset (s, 0, sizeof s);

// more code
}

I'd probably just make it an empty string

void foo()
{
char *s[42];
s[0] = 0;
// more code
}

or even:-

void foo()
{
char *s[42] = {0};
// more code
}

which actually just like the memset sets the whole string to zero.
And it's more readable.

But i would like to know the reason why/when/where it
should be used.

when you want to set a block of memory all to the same value.

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?

I've no idea what you mean. You use memset() when you want to set a
block of memory to the same value. If you don't want to do
that then don't call memset()!
 
M

Michael DOUBEZ

Jaydeep Chovatia a écrit :
So many times i have used memset to set character string to NULL
initially.

For automatic storage string, I would use array initialization
char str[42]={'\0'};

And for dynamic string, I would use std::fill_n:
std::fill_n(str,42,0);

As others said, you gain a lot by using std::string.
But i would like to know the reason why/when/where it
should be used.

Or 'if' it should be used (I never remember the order of the parameters
of memset).

IMO it is an old unfounded defensive programming technique.
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?

The only case I can think of is when using strncpy, the final \0 may not
be copied in some cases or if you generate the string content with an
algorithm pushing chars into it.
 
J

James Kanze

On 16 Dec, 05:58, Jaydeep Chovatia

[...]
when you want to set a block of memory all to the same value.

When you have to set a sequence of char types to the same value;
you can't use it for arbitrary values for anything but char
types. And when you have to set a sequence of integral types to
0; that works too. Those are the only cases where it is
guaranteed to do something reasonable.

std::fill, std::fill_n, std::uninitialized_fill and
std::uninitialized_fill_n are guaranteed to work in all cases,
and will generally work better than memset even in the cases
where memset works. So the answer to his actual question
(why/when/where memset should be used) is never.
 
R

Roman A. Kirillov

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.

As far as I understand, this is mostly just to be on the safe side -
you know that there's NULL there, whatever environment is, it is NULL
in debug, it is NULL in release, and it'll always behave in the same
way.

Regards,
Roman
http://sigizmund.com
 
A

Andrey Tarasevich

Jaydeep said:
So many times i have used memset to set character string to NULL
initially.

While not immediately illegal, the mention of NULL in this context is
misguided at best. What do you mean by 'setting character string to NULL'?
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?

We probably shouldn't. What makes you think we should? 'memset' has its
uses, but definitely not with character strings. On the second thought,
it can be used to generate long sequences of repeating characters. But
in this case the characters definitely wouldn't be '\0'. When it comes
to strings, generating long sequences of '\0' makes absolutely no sense,
since just one '\0' is always enough.

So, once again what is the implied connection between 'memset' and '\0'
you seem to talk about?
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top