cpy functions

Discussion in 'C Programming' started by Bill Cunningham, Apr 28, 2014.

  1. Is there a specific reason for using strcpy and memcpy for different
    uses? They look to me like the same thing.

    Bill Cunningham, Apr 28, 2014
    1. Advertisements

  2. Bill Cunningham

    John Gordon Guest

    strcpy() takes two arguments. memcpy() takes three arguments. How can they
    look the same?
    John Gordon, Apr 28, 2014
    1. Advertisements

  3. strcpy() copys C strings - i.e. it stops as soon as it encounters
    a terminating NULL character. memcpy() copies the specified number
    of bytes regardless of the contents of those bytes.
    Andrew Smallshaw, Apr 28, 2014
  4. You are ulminately moving values. If I've been understanding right. You
    can use casts with memcpy and pretty much move strings. Everything a value
    in memory. I have never used these two functions. I was just wondering if
    anyone out there has and what's the practical uses of having two functions
    that do the samething it seems to me anyway.

    Bill Cunningham, Apr 28, 2014
  5. Bill Cunningham

    Geoff Guest

    They are quite different.

    As the name implies, strcpy expects a valid C string in the source
    argument. A C string is a string of non-zero characters terminated by
    a zero character. If it's not a valid C string it will copy
    everything, including data you don't expect, up to the first zero
    char, into the destination. This can overflow your destination,
    leading to unexpected and unintended behavior in your program.

    memcpy is somewhat more safe. Memcpy doesn't manipulate C strings,
    only bytes, it knows nothing of zero termination it copies without
    judgement of the content it's copying. It takes three arguments, a
    destination, a source and a length. As long as length is <= the size
    of destination the copy will only copy the number of bytes that
    destination is expected to hold. Of course, you can still create
    problems for yourself if the source and destinations overlap or if you
    use a length greater than the destination.
    Geoff, Apr 28, 2014
  6. [...]
    char *a="String one";
    char *b="String two";


    Is that a "strcpy" like representation? Atleast to an extent.
    Bill Cunningham, Apr 28, 2014
  7. Bill Cunningham

    Geoff Guest


    That is wrong on two levels:
    1. You don't SUM two lengths to get the destination length.
    2. You don't SUM two pointers to get the length of anything.
    No, it's not even close.
    If you are going to use strings explicitly then use strcpy.
    Geoff, Apr 28, 2014
  8. It can also read beyond the valid bounds of the source.
    ....or greater than the source.

    I'm not sure you can say one is any safer (or less safe) than the other.
    Ben Bacarisse, Apr 28, 2014
  9. They're not. RTFM.
    Keith Thompson, Apr 28, 2014
  10. [...]

    Sorry about my bad code. I am saying a "C String" *is* a bytestream. void*
    can take the char * and You can manipulate that 3rd parameter and use use
    memcpy. For example.

    strtol (a,NULL,10)

    is essentially

    excepting aoti doesn't return error code. So we need strtol. And you can
    course convert to all but some of the str in strtol.

    strtol (a,b,10);

    Am I getting this out? C Strings are /unraw/ C bytes. They can /somehow/
    represent strings and be used like strcpy.

    K That's I'm done. I can't really and any more.

    Bill Cunningham, Apr 28, 2014
  11. No, you are not understanding correctly. No cast is needed to copy a
    string using memcpy.
    I wonder if there is anyone out there who has not -- they must be
    amongst the most used C functions. Anyway, the reasons to use one rather
    than the other derive from the differences between them that are spelt
    out in the specification or the man pages. (I'm not being cryptic, I
    just think there is more chance you'll remember the differences if you
    find out for yourself.)
    Read the man pages and try to work out why for yourself. In fact, I'd
    advice you not to read any more answers in the thread until you do,
    because the strategy you seem to have been using so far (which is to ask
    for explicit answers) does not seem to work for you.
    Ben Bacarisse, Apr 28, 2014
  12. Right yes. That's a generic pointer I mispoke. No casts needed.
    Bill Cunningham, Apr 28, 2014
  13. For some reason today. I can't type. That's supposed to read /raw/
    bytes. Wheww.
    Bill Cunningham, Apr 28, 2014
  14. I will.
    Bill Cunningham, Apr 28, 2014
  15. With memcpy, I think it would be the job of the kernel to make sure the
    memory areas don't overlap. That's what memory management services are for.
    Bill Cunningham, Apr 28, 2014
  16. NULL is (a macro that expands to) a null *pointer* constant. You're
    thinking of the null character (sometimes called NUL), '\0'.

    (Unfortunately, due to a quirk in the way null pointer constants are
    defined, you can sometimes get away with using NULL to represent a null
    Keith Thompson, Apr 28, 2014
  17. Bill Cunningham

    Ike Naar Guest

    * Bill Cunningham: An important member (holding the rank of inspector) of an unspecified, quite
    possibly fictional secret service force (possibly based upon the British Secret Intelligence
    Service). His most prominent bodily feature is his half-bald head. He meets the children upon
    their very first adventure and makes regular appearances in the series from that point on. Mostly
    the children get tangled up in adventures which are connected with Bill's work at the time and end
    up solving them for him.
    Following the events in The Ship of Adventure, Bill marries Mrs Mannering and adopts all the
    children as his own. Upon first encountering the children, he used the alias "Bill Smugs" - a name
    which appeared again in The Mountain of Adventure.

    Ike Naar, Apr 28, 2014
  18. Bill Cunningham

    Kaz Kylheku Guest

    Material this good can only come from having great staff.

    Kudos to the talented writers behind "Late Night with Bill Cunningham"!
    Kaz Kylheku, Apr 28, 2014
  19. [...]

    I'm not a C programmer. Just a wannabe and that's because the *nix system
    interface is in C. Otherwise C++ would probably be much simpler. But when
    you got a goal you have to take steps. I can sense C is very powerful and

    Bill Cunningham, Apr 28, 2014
  20. Bill Cunningham

    jacob navia Guest

    Le 28/04/2014 23:55, Kaz Kylheku a écrit :
    +1 !!!!!!!
    jacob navia, Apr 28, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.