Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!bbn!cc5.bbn.com!keesan From: keesan@cc5.bbn.com.UUCP Newsgroups: comp.lang.c Subject: Re: Copying a constant number of bytes Message-ID: <1790@cc5.bbn.com.BBN.COM> Date: Mon, 15-Jun-87 17:21:10 EDT Article-I.D.: cc5.1790 Posted: Mon Jun 15 17:21:10 1987 Date-Received: Thu, 18-Jun-87 01:44:34 EDT References: <900@bloom-beacon.MIT.EDU> Reply-To: keesan@bbn.com (Morris M. Keesan) Organization: Bolt Beranek and Newman, Cambridge, MA Lines: 27 Summary: structure assignment may be LESS efficient In article <900@bloom-beacon.MIT.EDU> newman@athena.mit.edu (Ron Newman) writes: >I want to copy a constant number of bytes NBYTES from address "a" >to address "b", where "a" and "b" are pointers of unspecified type. . . . >Method 1) > bcopy ((char *)a, (char *)b, NBYTES); > >Method 2) > struct nbytes {char s[NBYTES];}; > *(struct nbytes *)b = *(struct nbytes *)a; I will repeat the advice of others, which is to use bcopy. Portability and questionable casts aside, the structure assignment may be less efficient. The compiler I'm most familiar with does structure assignments by generating a function call to the function "strasg". What does strasg do? It calls bcopy, but because bcopy doesn't return a value and assignments do [the value of an assignment expression is the value of the lhs after the assignment], it returns its first argument. So instead of getting a simple call to bcopy (which is in MICROCODE on this machine), you end up taking the address of the two structures and getting an extra subroutine call, not to mention the overhead of pushing a return value on the stack and then dereferencing it to get the structure-valued return. Even if the optimizer undoes some of this, you've still got the overhead of the extra function call. -- Morris M. Keesan keesan@bbn.com {harvard,decvax,ihnp4,etc.}!bbn!keesan