[comp.unix.xenix] 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

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.