[comp.lang.c] Looking for memcpy

tony@joshua.math.ucla.edu (02/15/89)

Can somebody e-mail me C routines that copies (or compares) two
structures of the same type?

This is for somebody running Xenix on an ALTOS and these routines are not 
part of the standard library.

Thanks in advance.


Tony

henry@utzoo.uucp (Henry Spencer) (02/16/89)

In article <463@sunset.MATH.UCLA.EDU> tony@MATH.UCLA.EDU () writes:
>Can somebody e-mail me C routines that copies (or compares) two
>structures of the same type?

Copying is fairly trivial; in fact, struct assignment is part of most
C compilers (although if he's got a real antique it may not be there).	
Just "a = b;" where a and b are structs of the same type.

Nobody is going to be able to mail you C routines that reliably and
portably compare two structures of the same type, because it can't be
done without a member-by-member comparison.  The compiler is entitled
to insert "holes" into a structure to meet alignment requirements for
following members, and there is no guarantee that those holes will
contain the same data in all structs, so a simple compare-N-bytes
function is not good enough.
-- 
The Earth is our mother;       |     Henry Spencer at U of Toronto Zoology
our nine months are up.        | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

bagpiper@oxy.edu (Michael Paul Hunter) (02/26/89)

In article <463@sunset.MATH.UCLA.EDU> tony@joshua.math.ucla.edu writes:
>Can somebody e-mail me C routines that copies (or compares) two
>structures of the same type?
>


If your compiler will pass structures you can create a routine like the
one which follows to copy structures.

struct foo CopyStruct(struct foo Thing)
{
  return(Thing) ;
  }


Now if you don't want to write a routine for each type of structure you
wish to copy you could just copy it char by char.  As far as comparing,
I think there might be a probablem in comparing byte by byte in that
their could be unused space (well it is used for alignment) which might
contain different values even though two different structure are the same
member for member.  What does the proposed ANSI spec say (BTW, when well
it not be proposed anymore?) and what do older compilers do?

					  Mike

gwyn@smoke.BRL.MIL (Doug Gwyn ) (02/26/89)

In article <20598@tiger.oxy.edu> bagpiper@oxy.edu (Michael Paul Hunter) writes:
>In article <463@sunset.MATH.UCLA.EDU> tony@joshua.math.ucla.edu writes:
>>Can somebody e-mail me C routines that copies (or compares) two
>>structures of the same type?
>If your compiler will pass structures you can create a routine like the
>one which follows to copy structures.

The easiest way to copy a struct is to use assignment (=).

>I think there might be a probablem in comparing byte by byte in that
>their could be unused space (well it is used for alignment) which might
>contain different values even though two different structure are the same
>member for member.

Not only that, but if you're testing for anything but exact equality
you'd probably get the wrong results by comparing bytes instead of the
actual object members.  Also note that string members should be
compared via strcmp() and not just for pointer equality or as arrays.

>What does the proposed ANSI spec say (BTW, when well it not be proposed
>anymore?) and what do older compilers do?

The pANS requires support for struct assignment, struct function parameters,
and struct function return values.  These have actually been implemented in
most C compilers since around 1978.  Struct comparison is not supported,
because there is no unambiguously correct way to do it, as noted above.
The application itself must implement whatever is appropriate for this.

The proposed ANSI C standard is being reviewed by X3, with balloting to
end early in March.  If X3 approves it, the proposed standard will then
be sent to ANSI and it is unlikely in the extreme that it would not be
accepted as the official ANS for C at that point.

md@sco.COM (Michael Davidson) (03/01/89)

In article <20598@tiger.oxy.edu> bagpiper@oxy.edu (Michael Paul Hunter) writes:
>If your compiler will pass structures you can create a routine like the
>one which follows to copy structures.
>
>struct foo CopyStruct(struct foo Thing)
>{
>  return(Thing) ;
>  }
If your compiler is capable of handling function arguments and
return values which are structures it almost certainly implements
structure assignment, so what you want is as simple as:

struct foo x;
struct foo y

	x = y;

The only way to compare structures in a portable manner is to
compare corresponding members of the structures one by one.