memcpy() Behaviour

J

Jonathan Bartlett

im very confused about using memcpy and i have three
questions....memcpy takes a pointer to src and a pointer to dest and
copies src to destination...but im very confuzed about when to use '&'
operator while using memcpy....i have code that use '&' and the code
that call memcpy without '&'

It appears that you are confused on _pointers_ and not on memcpy.
memcpy takes two _pointers_ and copies the data from one set of memory
locations to another. If you don't understand what a pointer is or
does, you're screwed to start with.
one more thing how can i use macros in memcpy
like if i have
#define TRUE 1
then how can i copy TRUE into s1->name.

Not using memcpy. TRUE doesn't exist anywhere in memory, therefore
memcpy would be unable to find it.
Moreover , if i copy two INTS as in
int a;
int b;
memcpy(a,b,sizeof(int));
memcpy(&a,&b,sizeof(int));
Though i have not tested above but as far as i remember both of the
above memcpy lines will compile and work..aint it koz of the reason

No, they won't both work. The first one is completely wrong. The
second one should work (of course, you should initialize them first :])

The first one has no pointer to the memory -- it just gets whatever
value is in a & b. If a & b are 1 and 27, it would try to copy the
_contents_ of memory location 1 into memory location 27, neither of
which have anything to do with the values or memory locations of a and b.

If you're having trouble with pointers, perhaps you should learn
assembly language -- I've found that learning assembly language makes
pointers a lot easier to understand. You can check out my assembly
language book in my sig.

Jon
 
M

myhotline

hi all

im very confused about using memcpy and i have three
questions....memcpy takes a pointer to src and a pointer to dest and
copies src to destination...but im very confuzed about when to use '&'
operator while using memcpy....i have code that use '&' and the code
that call memcpy without '&'
like is the following same
Quest1
---
struct str{
int name;
}
struct stru{
int aname;
}
str *s;
s->name = 10;
stru *s1;
s1->aname = 100;
//now what about following two lines
memcpy(s->name,s1->aname,sizeof(int));
memcpy(&(s->name),&(s1->aname),sizeof(int));

if above two memcpy are same why? if not, why???
Quest2
-------
one more thing how can i use macros in memcpy
like if i have
#define TRUE 1
then how can i copy TRUE into s1->name.
Quest3
---------
Moreover , if i copy two INTS as in
int a;
int b;
memcpy(a,b,sizeof(int));
memcpy(&a,&b,sizeof(int));
Though i have not tested above but as far as i remember both of the
above memcpy lines will compile and work..aint it koz of the reason
that memcpy upon receving arguments cast them......???(well i have no
idea :( )...


...Thanks a lot for your time ..please revert as soon as possible

Greetings and Happy New Year..
Waqas
 
B

Ben Pfaff

im very confused about using memcpy and i have three
questions....memcpy takes a pointer to src and a pointer to dest and
copies src to destination...but im very confuzed about when to use '&'
operator while using memcpy....i have code that use '&' and the code
that call memcpy without '&'

Usually it depends on what you want to copy.
like is the following same
Quest1
---
struct str{
int name;
}
struct stru{
int aname;
}
str *s;
s->name = 10;
stru *s1;
s1->aname = 100;
//now what about following two lines
memcpy(s->name,s1->aname,sizeof(int));

Your compiler should complain about that. Neither s->name nor
s1->aname is a pointer, so you can't pass them as pointers.

(If your compiler doesn't complain, you probably forgot to
#include said:
memcpy(&(s->name),&(s1->aname),sizeof(int));

This will copy s1->aname to s->name, as if by assignment via
`s->name = s1->aname'. This is because you passed the addresses
of two `int's and told memcpy() to copy a `int' worth of bytes.
Quest2
-------
one more thing how can i use macros in memcpy
like if i have
#define TRUE 1
then how can i copy TRUE into s1->name.

You can't do it directly. The most straightforward way would be
not to use memcpy() at all. Instead, just write `s1->aname =
TRUE'. Alternatively, you can assign the macro's value to a
variable and then copy the variable:
int true_value = TRUE;
assert(sizeof s1->aname == sizeof true_value); /* Optional. */
memcpy(&s1->aname, &true_value, sizeof s1->aname);
Quest3
---------
Moreover , if i copy two INTS as in
int a;
int b;
memcpy(a,b,sizeof(int));
memcpy(&a,&b,sizeof(int));

Again, the former won't compile because neither a nor b is a
pointer. The latter is equivalent to a = b;
 
M

Michael Mair

hi all

im very confused about using memcpy and i have three
questions....memcpy takes a pointer to src and a pointer to dest and
copies src to destination...but im very confuzed about when to use '&'
operator while using memcpy....i have code that use '&' and the code
that call memcpy without '&'
like is the following same

True: You are confused.
Your problem is that you are not very sure when it comes to
C syntax and semantics.

That you did not try to compile the stuff you posted, does not
speak for your willingness to make it easy for _us_ to help
you.

Syntax error, missing semikolon.
struct stru{
int aname;
}
Dito.

str *s;

We are not in C++. You need to declare s as
struct str *s;
s->name = 10;

You did not get the memory s points to first.
Consider.

struct str *s, o;

s = &o;
s->name = 10;

stru *s1;
> s1->aname = 100;

Same as with "str *s;", plus:
Declarations (with optional initialization) go _first_ in
C89 and you must not put ist last.

//now what about following two lines
memcpy(s->name,s1->aname,sizeof(int));
memcpy(&(s->name),&(s1->aname),sizeof(int));

The first one will not compile.
if above two memcpy are same why? if not, why???

They are not.
My guess is that you have problems with arrays and
pointers; if we have a type T and declare

T arr_a[10], arr_b[10], *arrptr;

then we could pass 'arr_a' and 'arr_b' to memcpy
because the array name alone decays into a pointer
to the first element, that is, 'arr_a' is
equivalent to '&arr_a[0]'.
Read the comp.lang.c FAQ, especially the section
about pointers and arrays.
Quest2
-------
one more thing how can i use macros in memcpy
like if i have
#define TRUE 1
then how can i copy TRUE into s1->name.

A symbolic constant defined by a macro is a constant
or a constant expression. You cannot take the address
of constant expressions. What is the address of 2?
If you want to copy it, do so using '='.
Quest3
---------
Moreover , if i copy two INTS as in
int a;
int b;
memcpy(a,b,sizeof(int));
memcpy(&a,&b,sizeof(int));
Though i have not tested above but as far as i remember both of the
above memcpy lines will compile and work..aint it koz of the reason
that memcpy upon receving arguments cast them......???(well i have no
idea :( )...

If this compiles without error, then you are using a compiler which
is not compliant to the C89 or C99 standard and at best broken.

..Thanks a lot for your time ..please revert as soon as possible

Consider posting compilable code instead of wild guesses. This
will get you much more useful replies and you will learn much
by it.


-Michael
 
B

Barry Schwarz

hi all

im very confused about using memcpy and i have three
questions....memcpy takes a pointer to src and a pointer to dest and
copies src to destination...but im very confuzed about when to use '&'
operator while using memcpy....i have code that use '&' and the code
that call memcpy without '&'

You use the & when you want to copy to/from the variable itself,
regardless of its type.

You omit the & when the value of the variable is the address you want
to copy from/to. In order for the value to be an address, obviously
the variable must be a pointer. (Remember that an unsubscripted array
name passed to a function is converted to a pointer to the first
element of the array.)
like is the following same
Quest1
---
struct str{
int name;
}
struct stru{
int aname;
}
str *s;
s->name = 10;

A different problem than your question. s has not been initialized to
point to a struct. Therefore, any attempt to dereference s, as in
s->, invokes undefined behavior.
stru *s1;
s1->aname = 100;
//now what about following two lines
memcpy(s->name,s1->aname,sizeof(int));

s->name has type int, not type pointer. If there is a prototype in
scope for memcpy your compiler is required to generate a diagnostic
telling you the argument has the wrong type.
memcpy(&(s->name),&(s1->aname),sizeof(int));

This is syntactically correct. The parentheses following each & are
unnecessary but also harmless.
if above two memcpy are same why? if not, why???

One compiles, one doesn't.
Quest2
-------
one more thing how can i use macros in memcpy
like if i have
#define TRUE 1
then how can i copy TRUE into s1->name.

The macro substitution occurs before the code is compiled. The
compiler will not see the TRUE, only the 1. 1 is an integer constant
and you cannot refer to its address. Since memcpy requires an
address, the obvious answer to your question is you cannot. If you
elect to initialize an int to the value TRUE, then you can use memcpy
to copy from that int.
Quest3
---------
Moreover , if i copy two INTS as in
int a;
int b;
memcpy(a,b,sizeof(int));

Since it didn't work above, it won't work here either
memcpy(&a,&b,sizeof(int));

Another side problem. You never initialized b so attempting to copy
its contents to a invokes undefined behavior.
Though i have not tested above but as far as i remember both of the
above memcpy lines will compile and work..aint it koz of the reason

Don't assume. How much work would it have been to test this false
assertion?
that memcpy upon receving arguments cast them......???(well i have no
idea :( )...

When you call a function with a prototype in scope, the compiler will
attempt to convert the arguments to the type needed by the function,
IF AND ONLY IF such implicit conversion is allowed. memcpy wants a
pointer. a and b are both int. There is no implicit conversion
between int and pointer. If you really want to do this you must
supply the cast yourself but that opens up a whole different can of
worms which is way outside the scope of your question.



<<Remove the del for email>>
 
M

myhotline

Thank you all for your time and replies...i think i have understood
memcpy..Ben pfaff reply is very helpful , thanx ben......jonathan
Bartlett was right, i was confused on pointers and not on
memcpy.anyways thank you all
Greetings and Happy New Year..
Waqas
 
D

Dave Thompson

On 30 Dec 2004 13:57:56 -0800, (e-mail address removed) wrote:
im very confused about using memcpy [and pointers ...]
You use the & when you want to copy to/from the variable itself,
regardless of its type.

You omit the & when the value of the variable is the address you want
to copy from/to. <snip>

A different problem than your question. s has not been initialized to
point to a struct. Therefore, any attempt to dereference s, as in
s->, invokes undefined behavior.
Technically just fetching an uninitialized pointer value is UB. But
the UB of dereferencing it, especially for write, will more often
cause trouble than the UB of just fetching it, which on most systems
actually works though not required.

Since it didn't work above, it won't work here either


Another side problem. You never initialized b so attempting to copy
its contents to a invokes undefined behavior.
No, memcpy copies the object as array of unsigned char, which is not
allowed to have padding or trap representations, so even (the contents
of) uninitialized memory can be copied safely -- though uselessly,
since the copy is equally unusable in its real type.


- David.Thompson1 at worldnet.att.net
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top