473,602 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memcpy() Behaviour

hi all

im very confused about using memcpy and i have three
questions....me mcpy takes a pointer to src and a pointer to dest and
copies src to destination...b ut 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(i nt));
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,size of(int));
memcpy(&a,&b,si zeof(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......???(w ell 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 2922
my*******@gmail .com writes:
im very confused about using memcpy and i have three
questions....me mcpy takes a pointer to src and a pointer to dest and
copies src to destination...b ut 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(i nt));
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,size of(int));
memcpy(&a,&b,si zeof(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....me mcpy takes a pointer to src and a pointer to dest and
copies src to destination...b ut 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(i nt));
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,size of(int));
memcpy(&a,&b,si zeof(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......???(w ell 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....me mcpy takes a pointer to src and a pointer to dest and
copies src to destination...b ut 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,size of(int));
memcpy(&a,&b,si zeof(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....m emcpy takes a pointer to src and a pointer to dest and
copies src to destination...b ut 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(i nt));
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,siz eof(int));
Since it didn't work above, it won't work here either
memcpy(&a,&b,s izeof(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......???(w ell 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......jonath an
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,siz eof(int));


Since it didn't work above, it won't work here either
memcpy(&a,&b,s izeof(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.ne t
Nov 14 '05 #7

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

Similar topics

2
1848
by: rinku24 | last post by:
What is the difference between memcpy and memmove? Which is more expensive?
35
12030
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; char dst; memcpy( dst, src, 1 ); memcpy( NULL, src, 1 );
7
12067
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 it using the GNU linker. I do not reference the symbol memcpy explicitly anywhere in the offending function. I have narrowed the offending code down to the initialisation of an array of pointers to char (i.e. an array of strings), which is given...
15
38137
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 memcpy(bptr, aptr, sizeof(myStruct));
33
33686
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
14
4102
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 a function that would not clobber the parameter? Just surprised me a little is all.
70
11608
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 bytes. I am stuck here.
29
4225
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 memcpy() a well-defined way of so doing? The code is written knowing that sizeof(unsigned long) == 4 in this instance. The code is somewhat contrived in order to provide a self-contained program that will compile and show the use of memcpy() I...
18
2688
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
7993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.