home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / os / msdos / programm / 10876 < prev    next >
Encoding:
Text File  |  1992-11-24  |  3.7 KB  |  92 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mksol!kannarr.dseg.ti.com!kannarr
  3. From: kannarr@mk1501.dseg.ti.com (Galen Kannarr)
  4. Subject: Re: Very puzzling struct problem!
  5. Message-ID: <kannarr.12.0@mk1501.dseg.ti.com>
  6. Sender: usenet@mksol.dseg.ti.com (Usenet News)
  7. Nntp-Posting-Host: kannarr.dseg.ti.com
  8. Organization: Texas Instruments Incorporated
  9. References: <1992Nov21.221745.23588@jarvis.csri.toronto.edu>
  10. Date: Mon, 23 Nov 1992 23:24:40 GMT
  11. Lines: 79
  12.  
  13. In article <1992Nov21.221745.23588@jarvis.csri.toronto.edu> ematias@csri.toronto.edu (Ummm... me!) writes:
  14. >From: ematias@csri.toronto.edu (Ummm... me!)
  15. >Subject: Very puzzling struct problem!
  16. >Date: 22 Nov 92 03:17:45 GMT
  17. >
  18. >   While working on a little arcade-action game in Borland C v2.0, I've
  19. >recently come across something very puzzling; perhaps someone can explain 
  20. >to me exactly what is going on, since I'm not an expert programmer by any  
  21. >stretch of the imagination.  The problem is explained below.
  22. >
  23. >   I have declared a structure which holds all of the various characteristics
  24. >of my sprites, and it looks something like this:
  25. >
  26. >typedef struct {
  27. >    int x, y;
  28. >    int x2,y2;
  29. >    int oldx[2], oldy[2];
  30. >    int dx, dy;
  31. >    int width, height;
  32. >    int dead;
  33. >    int frame;
  34. >    int type;
  35. >    int whatever[1];
  36. >} Sprite;
  37. >
  38. >   The different elements of the structure aren't important -- only the
  39. >fact that there are 16 integer variables defined (i.e., 32 bytes).
  40. >   A configuration like the one above works fine; however, I found that when
  41. >I add or subtract an element from the struct (e.g., removing "int type", or
  42. >changing the bottom one to "int whatever[2]"), two things would happen:
  43. >
  44. >1)  My program's .EXE size would increase by more than 1000 bytes,
  45. >2)  The number of frames per second would slow down drastically!
  46. >    (i.e., the CPU was required to do much more work.)
  47. >
  48. >   Upon fiddling around with the struct elements, I noticed that the two
  49. >problems mentioned above would NOT occur when the number of elements was
  50. >a multiple of 16 -- but only for some multiples.  For example:
  51. >
  52. >- having 16 integer variables in the struct would work.
  53. >- having 32 integer variables in the struct would work.
  54. >- having 48 integer variables in the struct would NOT work.
  55. >- having 64 integer variables in the struct would work.
  56. >- having 80 integer variables in the struct would NOT work.
  57. >- having 96 integer variables in the struct would NOT work.
  58. >- having greater than 96 ints in the struct would NOT work.
  59. >
  60. >   This is totally baffling me, since I can't comprehend why my program
  61. >would be BIGGER and SLOWER just because the number of elements in a struct
  62. >isn't a multiple of 16 -- and furthermore, why it works fine for only
  63. >a select few multiples of 16.  Help!
  64. >
  65. >   Any comments or suggestions would be most helpful, and if I'm naively
  66. >overlooking something then a swift boot to the head would be in order. :-)
  67. >
  68. >Thanks,
  69. >
  70. >     - Mark Rosteck -
  71. >(ematias@csri.toronto.edu)
  72. >(g1marker@cdf.toronto.edu)
  73. >
  74. >
  75. >(By the way, I'm running this on a 33Mhz 386 and using the Compact memory
  76. >model, if this helps to clarify the problem.)
  77.  
  78. If I assume correctly that you are indexing off a pointer to an array of 
  79. these structs, I imagine that the compiler is optimizing the memory offset 
  80. caculation when the size of an element (the struct) is a power of two.  For 
  81. that special case, it can do a left shift by four to get the memory offset 
  82. for your 16-byte struct but executes a multiply instruction to figure 
  83. the offset if each struct takes up 14 bytes.  The multiply is at least four 
  84. times as slow as the shift, probably more if your index gets very large.
  85.  
  86. --
  87. Galen Kannarr    kannarr@dseg.ti.com
  88. --
  89. Galen Kannarr
  90. kannarr@mk1501.dseg.ti.com
  91. Texas Instruments Incorporated
  92.