home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11587 < prev    next >
Encoding:
Text File  |  1992-07-25  |  2.4 KB  |  75 lines

  1. Path: sparky!uunet!mcsun!news.funet.fi!hydra!klaava!wirzeniu
  2. From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Variable length records
  5. Message-ID: <1992Jul25.091702.15993@klaava.Helsinki.FI>
  6. Date: 25 Jul 92 09:17:02 GMT
  7. References: <24137@castle.ed.ac.uk> <rbjmzg-.gurgle@netcom.com>
  8. Organization: University of Helsinki
  9. Lines: 64
  10.  
  11. gurgle@netcom.com (Pete Gontier) writes:
  12. >typedef struct
  13. >{
  14. >    short   count;
  15. >   float   data[ ];
  16. >}
  17. >foo;
  18. >
  19. >I refer to the second member as an "open array." Its behavior is this:
  20. >    o  it may appear only as the last member of a structure
  21. >    o  it does not contribute to the value returned by sizeof(foo)
  22. >   o  it may be addressed, of course, with an arbitrary index
  23.  
  24. Are there any particular reasons to not use
  25.  
  26.     typedef struct {
  27.         short count;
  28.         float data[1];
  29.     } foo;
  30.  
  31. (and similar over-allocation and using "too large" indexes) instead?
  32. This version only assumes that there won't be any problems for accessing
  33. elements outside the data array.
  34.  
  35. And, except for a minor gain in convenience for allocation and
  36. deallocation, is either of these preferable to
  37.  
  38.     typedef struct {
  39.         short count;
  40.         float *data;
  41.     } foo;
  42.  
  43. (and allocating the memory for data separately) which _is_ ANSI
  44. compliant?
  45.  
  46. >I got worried about its portability so I paged through some manuals.
  47. >Both major C compilers for the Mac support it. Turbo C and Microsoft
  48. >C for the PC support it. According to Harbison and Steele, ANSI does
  49. >*not* support it. And from experience I know that AT&T cfront 2.1 (the
  50. >most common "standard" for C++) does not support it. I'm a little bit
  51. >suprised that it's not in the ANSI standard, but after my research I
  52. >felt it was common enough that I'd use it with impunity. And now I've
  53.  
  54. >just checked with 'gcc' 1.39, and it doesn't like it. Perhaps 2.x does.
  55.  
  56. My gcc 2.2.2 under Linux doesn't seem to support it, at least with the
  57. default options:
  58.  
  59.     foo.c:3: field `data' has incomplete type
  60.  
  61. >Not knowing about records simply means you have to implement it yourself.
  62. >There are no problems, as long as your compiler cooperates, with UNIX.
  63.  
  64. Actually, I don't think any special compiler cooperation is required. 
  65. To output the last version above to a file you do:
  66.  
  67.     fwrite(&foo.count, sizeof(foo.count), 1, outfile);
  68.     fwrite(foo.data, sizeof(float), foo.count, outfile);
  69.  
  70. (with error checking, of course) and similarly to read it.  (You might
  71. wish to use text files, to achieve better data portability.)
  72.  
  73. --
  74. Lars.Wirzenius@helsinki.fi
  75.