home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- From: raph@panache.demon.co.uk (Raphael mankin)
- Path: sparky!uunet!pipex!demon!panache.demon.co.uk!raph
- Subject: Re: compilers and code generation (struct vs. array)
- Distribution: world
- References: <1ihl0vINNof1@life.ai.mit.edu>
- Organization: Solvfield Ltd.
- Reply-To: raph@panache.demon.co.uk
- X-Mailer: Simple NEWS 1.90 (ka9q DIS 1.19)
- Lines: 41
- Date: Fri, 8 Jan 1993 20:32:20 +0000
- Message-ID: <726525140snz@panache.demon.co.uk>
- Sender: usenet@demon.co.uk
-
- In article <1ihl0vINNof1@life.ai.mit.edu> sundar@ai.mit.edu writes:
-
- >Let's say a point is defined as
- > struct point {
- > double x, y;
- > }
- >or as
- >typedef double point[2];
- >
- >I'm interested in the usual operation on points. (i.e.
- >additions, subtractions, multiplications, co-ordinate access,
- >and transformations -- i.e. multiplication by a 2x2 or 3x3 matrix
- >which again can be represented as an array or struct).
- >
- >Now let's look at one of these operations in detail:
- >
- >point_add(p1, p2, p3)
- >struct point *p1, *p2, *p3;
- >{
- > p3->x = p1->x + p2->x;
- > p3->y = p1->y + p2->y;
- >}
- >
- >or as
- >point_add(p1, p2, p3)
- >double *p1, *p2, *p3;
- >{
- > p3[0] = p1[0] + p2[0];
- > p3[1] = p1[1] + p2[1];
- >}
- >
- The semantics of passing arrays and structures as arguments to functions differ.
- In one case you get copying and this will have an impact on execution time. It
- will also matter if you do something like:
- point_add(p1, p1, p1);
- In the case of the arrays you might over-write your input values before you have
- finished using them. (Not in the case of a simpel add, but think about a
- complex divide operator).
- --
- --------------
- Raphael Mankin Nil taurus excretum
-