Why memset should be used?

Discussion in 'C++' started by Jaydeep Chovatia, Dec 16, 2008.

  1. 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
    Jaydeep Chovatia, Dec 16, 2008
    #1
    1. Advertising

  2. Jaydeep Chovatia

    Ian Collins Guest

    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
    Ian Collins, Dec 16, 2008
    #2
    1. Advertising

  3. Jaydeep Chovatia

    Salt_Peter Guest

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


    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.
    Salt_Peter, Dec 16, 2008
    #3
  4. Jaydeep Chovatia

    Guest

    On 16 Dec, 05:58, 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.
    >
    > 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
    , Dec 16, 2008
    #4
  5. Jaydeep Chovatia

    Guest

    On 16 Dec, 05:58, Jaydeep Chovatia <> 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()!
    , Dec 16, 2008
    #5
  6. 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
    Michael DOUBEZ, Dec 16, 2008
    #6
  7. Jaydeep Chovatia

    James Kanze Guest

    On Dec 16, 11:25 am, wrote:
    > On 16 Dec, 05:58, Jaydeep Chovatia
    > <> 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
    James Kanze, Dec 16, 2008
    #7
  8. On Dec 16, 5:58 am, 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.
    >


    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
    Roman A. Kirillov, Dec 16, 2008
    #8
  9. 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
    Andrey Tarasevich, Dec 16, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. k-man
    Replies:
    4
    Views:
    4,514
    Shelley Hebert
    Dec 18, 2003
  2. Joe C
    Replies:
    5
    Views:
    8,838
    tom_usenet
    Aug 24, 2004
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,769
    Smokey Grindel
    Dec 2, 2006
  5. Frederick Ding

    Is memset used only for strings?

    Frederick Ding, Dec 4, 2005, in forum: C Programming
    Replies:
    17
    Views:
    594
    Alex Fraser
    Dec 4, 2005
Loading...

Share This Page