UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » difference between strcpy and memcpy

#1 2005-12-08 08:19 AM

jyothi.b
Member
Registered: 2005-12-08
Posts: 3

Re: difference between strcpy and memcpy

hello sir,
myself jyothi,
1> i need to know the difference between strcpy and memcpy, i wanted to know internally how it is different,plz answer me sir

2>why the const keyword doesn't work in linux platform,it gives just warning,can u plz tell me about this why is it so

with regards
jyothi

Offline

#2 2005-12-08 02:00 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,760
Website

Re: difference between strcpy and memcpy

1. strcpy() works on null-terminated strings, and has no length limitations;
memcpy() works on arbitrary data of a given fixed length...  Aside from that, they
both preform the same basic function: shifting data around in memory...  But, you
use memcpy() when you want to copy an exact amount of data (possibly containing
embedded null chars) from one location to another, and strcpy() when you want to
copy a variable-length null-terminated C string from one location to another...
(Actually, it's not very good practice to use strcpy() even for its intended use, unless
you can guarantee that the destination buffer is at least as large as the source
string...  You're better off with something that restricts the max copied length, so
you don't overflow the destination buffer...  But, there's still no standard function
that does this properly...  strncpy() doesn't really count, since it will simply refuse
to null-terminate the destination in the case that the length is exceded, which is
generally not very helpful...  Something like the BSD's strlcpy() is what's needed...)

2. I have no idea what you're talking about...  "const" works just fine, and is used
quite liberally in prototypes in many system headers...  (Grep for it under
"/usr/include/" if you don't believe me...)

Offline

#3 2005-12-09 06:17 AM

jyothi.b
Member
Registered: 2005-12-08
Posts: 3

Re: difference between strcpy and memcpy

thank u for u r quick reply sir,
       about const keyword i will explain to u now,
          when i make constant pointer i.e
       main()
        {
                 int i=20;
                 int const *p;
                 p=&i;
                *p=90;
                 printf("%d\n",*p);
        }
its giving me only warning that it is readonly but it is giving me the output as 90,so i wanted to ask y its not giving any error,but it works fine in DOS flatform.


sir i have some confusion on working with memcpy and memmove,i have worked on it but i am confused with working and their difference,can u please eloberate and explain me how it differ and how it works with an example


with regards
jyothi

Offline

#4 2005-12-09 02:06 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,760
Website

Re: difference between strcpy and memcpy

Well, when you do something like that, you're deliberately breaking the specs,
and what happens is usually completely undefined and left up to the compiler
designers to decide how to handle...  In your case, you've got a non-constant
int, and you're assigning a pointer to it to a pointer to a const int...  So, the
compiler informs you of what appears to be a bug in your program (dereferencing
the pointer to a const int and assigning to that location), but since it's fairly liberal
about it by default, it lets you do it anyway if you're set on ignoring its advice...
And, since the real int you were pointing at was NOT a true constant, it was able
to actually be modified...  If you made "i" a const (and either made it "static", or
moved it outside of main() so it's a global), then the compiler would store it in
read-only memory, so your attempt to assign to it would cause your app to
seg-fault...  So, basically, the compiler is doing nothing wrong in letting you get
away with the above stupidity...  Good C compilers ALWAYS let you shoot yourself
in the foot if you are determined to do so; if they didn't, they'd be too restrictive
to write useful code in...  The best they can do is warn you of your apparent
stupidity, and if you want to ignore them, that's your own business...  But, if that's
not hand-holding enough for you, you can always compile with "-Werror" (or,
whatever equivalent, if you're not using GCC), so that any warning is considered
an error which prevents compilation...

As for memcpy() vs. memmove(), the only difference is that the former may not
work properly when the source and destination buffers overlap, while the latter is
guaranteed to work in that situation...  Eg:

char buf[16];
char buf2[16];

memcpy (buf, buf2, 16); /* this is normal expected usage */
memmove (buf, buf2, 16); /* will work fine, but it's overkill and unnecessary */

memcpy (buf, buf + 4, 12); /* not guaranteed to work properly (but, probably will) */
memmove (buf, buf + 4, 12); /* guaranteed to work properly */
memcpy (buf + 4, buf, 12); /* almost certainly will NOT work properly */
memmove (buf + 4, buf, 12); /* guaranteed to work properly */

Offline

#5 2005-12-10 06:38 AM

jyothi.b
Member
Registered: 2005-12-08
Posts: 3

Re: difference between strcpy and memcpy

thanking u sir

Offline

  • Index
  • » C
  • » difference between strcpy and memcpy

Board footer

Powered by FluxBB