[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Why memset should be used?

Jaydeep Chovatia

12/16/2008 5:59:00 AM

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
8 Answers

Ian Collins

12/16/2008 6:19:00 AM

0

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

--
Ian Collins

Salt_Peter

12/16/2008 9:54:00 AM

0

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.

Nick Keighley

12/16/2008 10:19:00 AM

0

On 16 Dec, 05:58, 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.
>
> 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

Nick Keighley

12/16/2008 10:26:00 AM

0

On 16 Dec, 05:58, Jaydeep Chovatia <chovatia.jayd...@gmail.com> wrote:

> 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()!




Michael DOUBEZ

12/16/2008 12:01:00 PM

0

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.

--
Michael

James Kanze

12/16/2008 12:41:00 PM

0

On Dec 16, 11:25 am, nick_keighley_nos...@hotmail.com wrote:
> On 16 Dec, 05:58, Jaydeep Chovatia
> <chovatia.jayd...@gmail.com> wrote:

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

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

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Roman A. Kirillov

12/16/2008 5:08:00 PM

0

On Dec 16, 5: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.
>

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://sig...

Andrey Tarasevich

12/16/2008 6:59:00 PM

0

Jaydeep Chovatia wrote:
> 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?

--
Best regards,
Andrey Tarasevich