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

Thread: memset problem

  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Arrow memset problem

    Hi,
    Sorry for putting a c code.

    I am trying to use memset. But it is behaving in a strange manner.
    Code:
    int main()
    {
    	 char arr[99];
    	 char tt[] = "hello";
    	 
    	 memcpy(arr,tt,5);
    	 memset(arr+5,' ',94);
    	 printf("%s",arr);
    	 return 0;
    }
    The out put is giving hello blank space and some junk at the end.

    But as per my knowledge it should only print hello.
    I am using vs2005.

    Please clarify

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: memset problem

    You need to add 0 in the end of string, and not space:
    memset(arr+5,0,94);

  3. #3
    Join Date
    Jul 2007
    Posts
    249

    Re: memset problem

    Thanks Alex it worked.

    But one thing i want to know when i used memcpy(arr,tt,5); tt is null terminated so does the memcpy do not copy the null here

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: memset problem

    Quote Originally Posted by Rajesh1978 View Post
    Hi,
    Sorry for putting a c code.

    I am trying to use memset. But it is behaving in a strange manner.
    Code:
    int main()
    {
    	 char arr[99];
    	 char tt[] = "hello";
    	 
    	 memcpy(arr,tt,5);
    	 memset(arr+5,' ',94);
    	 printf("%s",arr);
    	 return 0;
    }
    The out put is giving hello blank space and some junk at the end.

    But as per my knowledge it should only print hello.
    I am using vs2005.

    Please clarify
    What are you trying to accomplish there? Why not just use strcpy?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: memset problem

    Quote Originally Posted by Rajesh1978 View Post
    Thanks Alex it worked.

    But one thing i want to know when i used memcpy(arr,tt,5); tt is null terminated so does the memcpy do not copy the null here
    You told it to copy 5 bytes. The null is in the sixth position.

  6. #6
    Join Date
    Jul 2007
    Posts
    249

    Re: memset problem

    I was trying to understand the behaviour of memset and memcpy.

    I had a misconception that it will not copy the null character.

    thanks

  7. #7
    Join Date
    Dec 2009
    Posts
    161

    Re: memset problem

    btw if I were you I'd always initialiaze vars with something:

    Code:
    char arr[99] = {0};

  8. #8
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: memset problem

    Quote Originally Posted by GCDEF View Post
    You told it to copy 5 bytes. The null is in the sixth position.
    And even if you had copied at, you would have overwritten it with a space next.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  9. #9
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: memset problem

    Quote Originally Posted by Rajesh1978 View Post
    I was trying to understand the behaviour of memset and memcpy.

    I had a misconception that it will not copy the null character.

    thanks
    It copies whatever you tell it to copy. hello is 5 bytes. You told the memcpy to copy 5 bytes. If you wanted it to copy the NULL then you tell it to copy 6 bytes. Or you do as others have suggested and use strcpy. memcpy doesn't care what is in the source memory block. It copies exactly how many bytes you tell it to copy irrespective of what is contained within that source memory block.

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