473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

memcpy() Behaviour

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
-------------------------------------
"You laugh at me because i am different, i laugh at you because you are
all the same." - Kurt Cobaine

Nov 14 '05 #1
6 2894
my*******@gmail.com writes:
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 <string.h>.)
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;

--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless
Nov 14 '05 #2
my*******@gmail.com wrote:
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.
Quest1
---
struct str{
int name;
}
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3
> 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
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #4
On 30 Dec 2004 13:57:56 -0800, my*******@gmail.com wrote:
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>>
Nov 14 '05 #5

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
-------------------------------------
"You laugh at me because i am different, i laugh at you because you are
all the same." - Kurt Cobaine

Nov 14 '05 #6
On Thu, 30 Dec 2004 18:59:39 -0800, Barry Schwarz <sc******@deloz.net>
wrote:
On 30 Dec 2004 13:57:56 -0800, my*******@gmail.com 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>

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.

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.

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

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
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rinku24 | last post by:
What is the difference between memcpy and memmove? Which is more expensive?
35
by: Christopher Benson-Manica | last post by:
(if this is a FAQ or in K&R2, I didn't find it) What parameters (if any) may be 0 or NULL? IOW, which of the following statements are guaranteed to produce well-defined behavior? char src;...
7
by: Richard Hayden | last post by:
Hi, I've just upgraded my gcc and I'm currently trying to compile some code for my own operating system kernel, but I am getting an error of "Undefined reference to `memcpy`" when I try to link...
15
by: Sourcerer | last post by:
Hi all. Can I do this to duplicate the contents of struct a in struct b struct myStruct a, b, *aptr, *bptr; aptr = a; bptr = b; Do something to initialize contents of structure a...
33
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
14
by: Michael B Allen | last post by:
I just noticed that doing something like the following may fail because it can overwrite u->size before it's evaluated. memcpy(u, buf, u->size); Is it legit that this is a macro as opposed to...
70
by: Rajan | last post by:
Hi, I am trying to simulate a memcpy like this void* mem_cpy(void* dest, void* src, int bytes) { dest = malloc(bytes); } Now if I want to copy the bytes from src to dest, how do I copy these...
29
by: Martin | last post by:
For reasons I won't go into, I need to transfer from 1 to 3 bytes to a variable that I know is 4 bytes long. Bytes not written to in the 4-byte target variable must be zero. Is the following use of...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.