cpy functions

B

Bill Cunningham

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

Bill
 
J

John Gordon

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

strcpy() takes two arguments. memcpy() takes three arguments. How can they
look the same?
 
A

Andrew Smallshaw

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

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.
 
B

Bill Cunningham

John Gordon said:
strcpy() takes two arguments. memcpy() takes three arguments. How can
they
look the same?
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
<shrug>
 
G

Geoff

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

Bill

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.
 
B

Bill Cunningham

[...]
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.

char *a="String one";
char *b="String two";

memcpy(a,b,sizeof(a+b));

Is that a "strcpy" like representation? Atleast to an extent.
 
G

Geoff

[...]
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.

char *a="String one";
char *b="String two";

memcpy(a,b,sizeof(a+b));
WTF!---------------^^^

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.
Is that a "strcpy" like representation? Atleast to an extent.
No, it's not even close.
If you are going to use strings explicitly then use strcpy.
 
B

Ben Bacarisse

Geoff said:
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.

It can also read beyond the valid bounds of the source.
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.

....or greater than the source.

I'm not sure you can say one is any safer (or less safe) than the other.
 
K

Keith Thompson

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

They're not. RTFM.
 
B

Bill Cunningham

[...]

WTF!---------------^^^

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.

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
atoi(a)

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
 
B

Ben Bacarisse

Bill Cunningham said:
You are ulminately moving values. If I've been understanding right. You
can use casts with memcpy and pretty much move strings.

No, you are not understanding correctly. No cast is needed to copy a
string using memcpy.
Everything a value
in memory. I have never used these two functions. I was just wondering if
anyone out there has

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.)
and what's the practical uses of having two functions
that do the samething it seems to me anyway.

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.
 
B

Bill Cunningham

Ben Bacarisse said:
No, you are not understanding correctly. No cast is needed to copy a
string using memcpy.

Right yes. That's a generic pointer I mispoke. No casts needed.
 
B

Bill Cunningham

Bill Cunningham said:
[...]

WTF!---------------^^^

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.

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
atoi(a)

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.

For some reason today. I can't type. That's supposed to read /raw/
bytes. Wheww.
 
B

Bill Cunningham

Keith Thompson said:
They're not. RTFM.

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.
 
K

Keith Thompson

Andrew Smallshaw said:
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.

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
character.)
 
I

Ike Naar

[...]
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.

char *a="String one";
char *b="String two";

memcpy(a,b,sizeof(a+b));

Is that a "strcpy" like representation? Atleast to an extent.

* 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.

https://en.wikipedia.org/wiki/Adventure_Series
 
B

Bill Cunningham

Bill Cunningham said:
"Geoff" <[email protected]> wrote in message
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.
[...]

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
versatile.

Bill
 
J

jacob navia

Le 28/04/2014 23:55, Kaz Kylheku a écrit :
Material this good can only come from having great staff.

Kudos to the talented writers behind "Late Night with Bill Cunningham"!
+1 !!!!!!!
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top