Find the answer to your Linux question:
Results 1 to 6 of 6
hi, can anyone explain me what strcpy() & memcpy() actually does?plzz reply me soon.........
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    19

    difference between strcpy() and memcpy()


    hi,

    can anyone explain me what strcpy() & memcpy() actually does?plzz reply me soon......

  2. #2
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401

    no difference

    Functionally there's no difference: both copy memory from a location to another...

    The main difference is the function declaration... probably also implementation differs, and memcpy should reasonably faster than strcpy, because strcpy must stop when encounter the first '\0' character, so it must test char by char (or something similar), while memcpy can optimize using memory alignement and other stuffs...
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  3. #3
    Just Joined!
    Join Date
    Sep 2005
    Location
    China
    Posts
    2
    Hi,In Chinese:

    strcpy包含两个参数,无法确定要拷贝的字节数,而只能通过识别'\0'来完成一次拷贝。
    memcpy的函数中包含要拷贝的字节数,80x86下是用rep movsb实现的,拷贝的速度相对要快些。

  4. $spacer_open
    $spacer_close
  5. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Functionally there's no difference
    Huh? One copies a block of memory, and the other copies a sequence of bytes, terminating on a NULL character. Quite a big difference, I'd have thought, if you're writing code that has to work.

  6. #5
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    while using memcpy be careful since u have to check the destination size and source size which ever is greater or smaller and according to that do the memcpy and insert the '\0' character at the end after the memcpy when copying strings

  7. #6
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    I just got an example of what i had done previously

    Its something like this

    memcpy(dest, src,sizeof(dst));
    if(strlen(src)>sizeof(dest))
    {
    dest[sizeof(dest)-1]='\0';
    }
    else
    {
    dest [strlen(src)]='\0';
    }

    same thing can be applicable for memmove but memory areas overlap in that

Posting Permissions

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