memcpy ( with address operator or not? ) 
Author Message
 memcpy ( with address operator or not? )

Hi,

Why there is no difference in end result of following memcpy commands. Are
they both correct ways to use memcpy?

A)     memcpy ( destination,  source->element, length)

B)    memcpy ( destination, &(source->element), length)

-Tomi



Sat, 28 Feb 2004 16:19:16 GMT  
 memcpy ( with address operator or not? )

Quote:

> Why there is no difference in end result of following memcpy commands. Are
> they both correct ways to use memcpy?

Is source->element an array type?

Quote:
> A)     memcpy ( destination,  source->element, length)
> B)    memcpy ( destination, &(source->element), length)

If it's an array, then these two are just about identical, because
using an array (in some contexts) yields a pointer to it's
first element. To use & on an array yields a pointer to the
whole array. The types are different, but the locations are the
same, and none of it matters once both have been shoved though a
void* type.

If this is not an array type, then A would only work if
source->element is a pointer (I think) and they would only be
identical if it's a self referencing pointer.

Bill, two sides to every story.



Sat, 28 Feb 2004 17:19:05 GMT  
 memcpy ( with address operator or not? )

Quote:
> Hi,
> Why there is no difference in end result of following memcpy commands. Are
> they both correct ways to use memcpy?
> A)     memcpy ( destination,  source->element, length)
> B)    memcpy ( destination, &(source->element), length)

Show us the definition of destination and source and we might have a
chance of helping you.
Normally these two forms are quite distinct.
For example, if you have:

struct foobar {
  int element;

Quote:
};

struct foobar *source;

then the form A above will try to interpret the integer as a pointer,
and quite likely cause undefined behaviour. The form B copies the data
into the element itself. This is the case of all element types except
arrays.
With arrays, like this:

struct foobar {
  int element[500];

Quote:
};

struct foobar *source;

the two forms could be equivalent, but I haven't tested it.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Normal is what everyone else is, and you're not."
   - Dr. Tolian Soran



Sat, 28 Feb 2004 17:10:32 GMT  
 memcpy ( with address operator or not? )
So, it is safer to use version B? I suppose then "nothing" can go wrong...
:-)

-Tomi


Quote:

> > Why there is no difference in end result of following memcpy commands.
Are
> > they both correct ways to use memcpy?

> Is source->element an array type?

> > A)     memcpy ( destination,  source->element, length)
> > B)    memcpy ( destination, &(source->element), length)

> If it's an array, then these two are just about identical, because
> using an array (in some contexts) yields a pointer to it's
> first element. To use & on an array yields a pointer to the
> whole array. The types are different, but the locations are the
> same, and none of it matters once both have been shoved though a
> void* type.

> If this is not an array type, then A would only work if
> source->element is a pointer (I think) and they would only be
> identical if it's a self referencing pointer.

> Bill, two sides to every story.



Sat, 28 Feb 2004 18:03:06 GMT  
 memcpy ( with address operator or not? )

Quote:
> So, it is safer to use version B? I suppose then "nothing" can go wrong...
> :-)
> -Tomi



>> > Why there is no difference in end result of following memcpy commands.
> Are
>> > they both correct ways to use memcpy?

>> Is source->element an array type?

>> > A)     memcpy ( destination,  source->element, length)
>> > B)    memcpy ( destination, &(source->element), length)

>> If it's an array, then these two are just about identical, because
>> using an array (in some contexts) yields a pointer to it's
>> first element. To use & on an array yields a pointer to the
>> whole array. The types are different, but the locations are the
>> same, and none of it matters once both have been shoved though a
>> void* type.

>> If this is not an array type, then A would only work if
>> source->element is a pointer (I think) and they would only be
>> identical if it's a self referencing pointer.

>> Bill, two sides to every story.

That depends on what you want to do. If you want to copy data from the
structure itself, you should always stick to version B, particularly
if you're not using an array.
If, however, you want to go one indirection level further and want to
copy data from a place that the structure points to, use version A.
For example:

struct foobar {
  char *element;

Quote:
};

struct foobar *source;
char destination[800];
source = malloc(sizeof(*source));
source->element="Hello, world!";
memcpy(destination, source->element, 14);

This will now copy the string "Hello, world!" into the first 14
bytes of the array destination. If you had &(source->element)
instead of source->element, the first 14 bytes of the array
destination would instead contain the *address of* the string
literal "Hello, world!" which would not be quite useful, particularly
if you intend to write the structure on disk or send it across a
network.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
   - Douglas Adams



Sat, 28 Feb 2004 18:27:21 GMT  
 memcpy ( with address operator or not? )

Quote:

> So, it is safer to use version B? I suppose then "nothing" can go wrong...

This is going to sound incredibly unhelpful, but if to use & is
appropriate, use &. If & is not appropriate, don't use &.

Without a wider context, I can't advise.

What type is source->element?

If it is an array, then use whichever one feels nicest. I would
prefer to use the first form. That way, you can change from an
array to a pointer later on, without any extra hassle.

Bill, up to you.

BTW, please trim the quoted text to just enough to establish context.
A complete duplication of the article is a waste.



Sat, 28 Feb 2004 18:43:38 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Not operator NOT working (!)

2. memcpy does not work !!

3. memcpy( NULL, NULL 0 ) - safe or not?

4. C Question - pointer & address-operator

5. anti-address operator

6. Why use address-of operator (&)??

7. The & (address) operator and register allocation

8. Address of vector::operator[n]() if instantiated with C-array type

9. IMPORTANT: IntelliSense does not work after __typeof operator

10. CXX0058: Error: Overloaded Operator Not Found

11. operator->() overload not being called

12. bool operator<(std::string) NOT FOUND

 

 
Powered by phpBB® Forum Software