Thread: Issue with memcpy

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    26

    Issue with memcpy

    Hi guys,

    I had an issue wrt memcpy in my code...Pls find the snippet of code below

    Code:
    #define ARR_LEN 100
    
    char arr_ca[ARR_LEN + 1 ];
    arr_ca[ARR_LEN]= {'\0'}; // Initialising the last element of the string
    memset(arr_ca,0x00,sizeof(arr_ca));
    memset(arr_ca, "Checking the text", ARR_LEN);
    When i debug the above prgm and check the value of arr_ca it gives
    "Checking the text \00\00\00 asjdlkjewiruioiuoiuasdsd .(some junk value).."

    I am not getting as to why am i getting junk value since i have already initiliased arr_ca using 0x00 in step 3...Can anyone explain me on this!!!
    Last edited by vandrea; 08-17-2009 at 12:17 PM. Reason: syntax error

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    What do you get when you execute this:
    Code:
    printf("Sizeof arr_ca is %i\n", sizeof(arr_ca));

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    Code:
    printf("Sizeof arr_ca is %i\n", sizeof(arr_ca));

    I get 101

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    memset isn't memcpy. why do you have memcpy in your title?

    what are you trying to do?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    memset(arr_ca, "Checking the text", ARR_LEN);
    That is where your junk values are coming from. Read up on memset(), and what it does.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    memset isn't memcpy. why do you have memcpy in your title?

    what are you trying to do?
    Sorry this should have been memcpy on line no 4 i.e.

    memcpy(arr_ca, "Checking the text", ARR_LEN); instead of memset

    I am assigning the character array with sum value!!
    I have used memcpy in my code, but then too it gives junk value...any idea?
    Last edited by vandrea; 08-17-2009 at 12:36 PM. Reason: additional info

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by vandrea View Post
    Sorry this should have been memcpy on line no 4 i.e.

    memcpy(arr_ca, "Checking the text", ARR_LEN); instead of memset

    I am assigning the character array with sum value!!
    That code is not valid (it is a buffer overrun). "Checking the text" is 18 bytes, but you are telling memcpy() to copy 100 bytes from this location. This means you copy in whatever random data is after the "Checking the text" data. I think you meant to use strcpy() instead of memcpy().
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    Thanks bithub for ur reply ..

    That code is not valid (it is a buffer overrun). "Checking the text" is 18 bytes, but you are telling memcpy() to copy 100 bytes from this location. This means you copy in whatever random data is after the "Checking the text" data. I think you meant to use strcpy() instead of memcpy().
    I did intend to use memcpy to know its behaviour while copying the data . But how is the random value coming since I would have done memset earlier and all the data in array would be initialised with
    Code:
    {'\0'}.
    So then when I do
    Code:
    memcpy
    I would just copy those 18 characters in first 18 places right without modifying the rest of the data right??? Pls enlighten me on this I might be wrong in understanding!!

    Also which ones better strcpy or memcpy in C?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Why are you initialising the array arr_ca[] so many times.
    Code:
    arr_ca[ARR_LEN]= {'\0'}; /* don't need this as it is handled by the memset() following it */

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by vandrea View Post
    Thanks bithub for ur reply ..



    I did intend to use memcpy to know its behaviour while copying the data . But how is the random value coming since I would have done memset earlier and all the data in array would be initialised with
    Code:
    {'\0'}.
    So then when I do
    Code:
    memcpy
    I would just copy those 18 characters in first 18 places right without modifying the rest of the data right??? Pls enlighten me on this I might be wrong in understanding!!

    Also which ones better strcpy or memcpy in C?
    Your memcpy statement says -- go get 100 bytes from the address given by "Checking the text" and copy it to this location. The rub being, there's not necessarily 100 bytes at that address for you to copy.

    Strcpy is used to copy strings, memcpy is used to copy chunks of memory. (Obviously that's a somewhat crude dichotomy, but it works surprisingly well.) Since in this case you don't want to copy a chunk of memory, but rather a string, memcpy is not the tool for you.

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    26

    Smile

    Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM