Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why memset should be used?

Reply
Thread Tools

Why memset should be used?

 
 
Jaydeep Chovatia
Guest
Posts: n/a
 
      12-16-2008
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-16-2008
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
 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      12-16-2008
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.
 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      12-16-2008
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


 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      12-16-2008
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()!




 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      12-16-2008
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-16-2008
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:
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
 
Reply With Quote
 
Roman A. Kirillov
Guest
Posts: n/a
 
      12-16-2008
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://sigizmund.com
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      12-16-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
"memset" vs "= {0}"...Are they equivalent if your initializing variables? Nollie@runtime.com C++ 17 09-22-2004 06:06 PM
memset vs fill and iterators vs pointers Joe C C++ 5 08-24-2004 11:51 AM
2 questions: speed of memset() and pointer to multi-arrays k-man C++ 4 12-18-2003 08:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57