CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: memset question

  1. #1
    Join Date
    Mar 2005
    Posts
    55

    memset question

    just a quick lil question on memset()
    if i do something like this

    char buf[128];

    memset(buf, 'A', sizeof(buf));

    will this overwrite the terminating character \0? since im filling the whole buffer
    with A or does memset automatically take into account the \0?
    because i kno that \0, \r, and \n only take "1 slot" in the buffer

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: memset question

    Quote Originally Posted by Alch3mist
    will this overwrite the terminating character \0?
    There never was a terminating 0 in your buffer.
    Quote Originally Posted by Alch3mist
    since im filling the whole buffer with A or does memset automatically take into account the \0?
    Yes, you are filling the whole thing with 'A'; No, memset doesn't take \0 into account
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Mar 2005
    Posts
    55

    Re: memset question

    ight so would this work better
    char buf[128] = 0;

    memset(buf, 'A', sizeof(buf)-1);

    or do i have to specify each time after lets say memset, memcpy, strcpy,strcat to place a \0 at the end of my buffer

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: memset question

    Quote Originally Posted by Alch3mist
    ight so would this work better
    char buf[128] = 0;
    Unless buf is declared as having 129 elements, that is a memory access violation.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: memset question

    I guess, he meant
    char buf[128] = {'\0'};
    which will be ok.

  6. #6
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: memset question

    Quote Originally Posted by Alch3mist
    will this overwrite the terminating character \0? since im filling the whole buffer with A or does memset automatically take into account the \0?
    because i kno that \0, \r, and \n only take "1 slot" in the buffer
    Only subset of standard C function (str* functions, formating funcitons as printf/scanf, fgets/fputs etc.) knows or cares about null-terminated 'strings' - for every other function (including memset) it is just array of chars, with \0 having no special meaning. So, when you want to set null-terminated string with memset, you have to add the terminating null yourself. Also keep in mind that you need space for the terminating null, so for 128-char string, you need array of size for 129 chars. It's mess, i know - if you have chance to use C++ and std::string, go for it

  7. #7
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715

    Re: memset question

    right so would this work better

    char buf[128] = 0;
    memset(buf, 'A', sizeof(buf));

    no that won't work cause of coarse your putting your null terminator in the first character of your array... Then you're overwriting that character with the memset... Try this..

    Code:
    char buf[128];
    
    memset( buff, '\0', sizeof(buff));
    memset( buff, 'A', sizeof( buff) -1 );
    or you could do this..

    Code:
    char buff[128];
    
    memset( buff, 'A', sizeof( buff ) -1 );
    buff[ sizeof(buff)-1 ] = '\0';
    [/code]

  8. #8
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715

    Re: memset question

    Quote Originally Posted by Paul McKenzie
    Unless buf is declared as having 129 elements, that is a memory access violation.

    Regards,

    Paul McKenzie
    Paul check that... when you initialize an array like that it always begins from the beginning....

    you must not have realized it was initializing from the variable declaration..


    thus

    Code:
    char buff[128] = 0;                          // is fine... possible casting issue
    char buff[128]  = "yada yad yada";  // is fine
    char buff[128] = '\0';                        // is also fine
    Last edited by JMS; December 21st, 2006 at 02:10 AM.

  9. #9
    Join Date
    Dec 2006
    Posts
    166

    Re: memset question

    Code:
    char buff[128] = 0;
    char buff[128] = '\0';
    no, these are not valid initializers for an array

  10. #10
    Join Date
    Oct 2005
    Posts
    199

    Re: memset question

    Quote Originally Posted by spoon!
    Code:
    char buff[128] = 0;
    char buff[128] = '\0';
    no, these are not valid initializers for an array
    That's correct. They only set the value in pos. 128 to 0, not the whole array. As it was stated before, you need brackets to set the array to 0 when declaring it.
    'You help me, and I, in turn, am helped by you!'
    -H.S.

  11. #11
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: memset question

    I think the solution from JMS is probably the best
    Code:
    char buff[128];
    
    memset( buff, 'A', sizeof( buff ) -1 );
    buff[ sizeof(buff)-1 ] = '\0';
    Also, call me crazy, but I recall when initializing arrays, you need to use the initializer list
    Code:
    char buff[128] = {'\0'};
    So I don't think either of these are correct for initializing an array
    Code:
    char buff[128] = 0;
    OR
    char buff[128] = '\0'
    Please rate my post if you felt it was helpful

  12. #12
    Join Date
    Dec 2006
    Posts
    154

    Re: memset question

    Quote Originally Posted by Paul McKenzie
    Unless buf is declared as having 129 elements, that is a memory access violation.

    Regards,

    Paul McKenzie
    compiler won't compile this or have an extension which allows defining that way, in this case it is treated exactly as
    Code:
    char buf[128] = { 0 };
    it will never be an access violation but it is for sure not standard. I believe only very few, obscure compilers implement this.

  13. #13
    Join Date
    Sep 2006
    Location
    Sunshine State
    Posts
    517

    Re: memset question

    I'm pretty sure that what Paul meant was
    Code:
    buff[128] = 0;
    would cause an access violation.
    Otherwise you are right, JMS.


    -Andy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured