Thread: A tiny question about memcpy

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    85

    A tiny question about memcpy

    Hello, I am new to C++.
    I know the use of function memcpy().
    but what is the difference between using memcpy(&str1, &str2, sizeof(str2)) and str1 = str2 ?

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    memcpy will copy the contents of an address for n bytes. str1 = str2 will assign the address str2 to str1 (assuming they're pointers).

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    Originally posted by Enmeduranki
    memcpy will copy the contents of an address for n bytes. str1 = str2 will assign the address str2 to str1 (assuming they're pointers).
    is memcpy() slower if I want to copy a exact variable ?

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >is memcpy() slower if I want to copy a exact variable ?

    You're not copying an exact variable by assigning one pointer to another. You're copying a pointer.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    Originally posted by Enmeduranki
    >is memcpy() slower if I want to copy a exact variable ?

    You're not copying an exact variable by assigning one pointer to another. You're copying a pointer.
    how if I using * before a pointer ?

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    If you're only copying sizeof(one primitive) then yes.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    Originally posted by Enmeduranki
    If you're only copying sizeof(one primitive) then yes.
    you mean memcpy() is slower ?

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    Almost definitely, but it'll be up to the implementation.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    Thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. Trying to copy buffers using memcpy in C under UNIX
    By Meeper in forum C Programming
    Replies: 3
    Last Post: 07-26-2003, 08:51 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM