home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19472 < prev    next >
Encoding:
Text File  |  1993-01-09  |  1.7 KB  |  56 lines

  1. Newsgroups: comp.lang.c
  2. From: raph@panache.demon.co.uk (Raphael mankin)
  3. Path: sparky!uunet!pipex!demon!panache.demon.co.uk!raph
  4. Subject: Re: compilers and code generation (struct vs. array) 
  5. Distribution: world
  6. References: <1ihl0vINNof1@life.ai.mit.edu>
  7. Organization: Solvfield Ltd.
  8. Reply-To: raph@panache.demon.co.uk
  9. X-Mailer: Simple NEWS 1.90 (ka9q DIS 1.19)
  10. Lines: 41
  11. Date: Fri, 8 Jan 1993 20:32:20 +0000
  12. Message-ID: <726525140snz@panache.demon.co.uk>
  13. Sender: usenet@demon.co.uk
  14.  
  15. In article <1ihl0vINNof1@life.ai.mit.edu> sundar@ai.mit.edu writes:
  16.  
  17. >Let's say a point is defined as
  18. >        struct point { 
  19. >                double x, y;
  20. >        }
  21. >or as
  22. >typedef double point[2];
  23. >
  24. >I'm interested in the usual operation on points. (i.e. 
  25. >additions, subtractions, multiplications, co-ordinate access,
  26. >and transformations -- i.e. multiplication by a 2x2 or 3x3 matrix 
  27. >which again can be represented as an array or struct).
  28. >
  29. >Now let's look at one of these operations in detail:
  30. >
  31. >point_add(p1, p2, p3)
  32. >struct point *p1, *p2, *p3;
  33. >{
  34. >        p3->x = p1->x + p2->x;  
  35. >        p3->y = p1->y + p2->y;  
  36. >}
  37. >
  38. >or as
  39. >point_add(p1, p2, p3)
  40. >double *p1, *p2, *p3;
  41. >{
  42. >        p3[0] = p1[0] + p2[0];
  43. >        p3[1] = p1[1] + p2[1];
  44. >}
  45. >
  46. The semantics of passing arrays and structures as arguments to functions differ.
  47. In one case you get copying and this will have an impact on execution time. It
  48. will also matter if you do something like:
  49.     point_add(p1, p1, p1);
  50. In the case of the arrays you might over-write your input values before you have
  51. finished using them. (Not in the case of a simpel add, but think about a
  52. complex divide operator).
  53. -- 
  54. --------------
  55. Raphael Mankin            Nil taurus excretum
  56.