Variable record size -- ANSI

GEMMELL d0np at jupiter.sun.csd.unb.ca
Fri Apr 12 12:14:05 AEST 1991


+ Michael N Johnston writes:
+ ]>struct foo {
+ ]>unsigned short recsize;
+ ]>unsigned short num;
+ ]>char info [24];
+ ]>byte flags;
+ ]>char filename[1]
+ ]>};
+ ]>Where, foo.filename is a placeholder for a variable length string.

+ ]If you plan to store a char array in filename DON'T. Use char *filename
+ ]and point this to your actual array. Using your origional declaration
+ ]and doing something like strcpy(foo_ptr->filename, "abcde") will lead
+ ]to disaster.

+ Why should this lead to disaster?   Is this not allowed by ANSI?
+ If you can make sure that you have malloced more than enough space for the
+ structure, shouldn't you be able to use all this space by doing just that?

  It would not lead to disaster if you do malloc enough space, but
I don't think doing a malloc like 
   
  t = (struct foo *)malloc( sizeof( struct foo ) + 10 (or + var, etc) );

would be all that useful.  If you want to increase the size of your string
you would have to reallocate a new structure, copy over the
old data.  I agree with the use of char *filename, it would require a little
less overhead, if you just have to allocate or reallocate space for a string.

             --------------------------------------------
             |                                          |
             | \ \ \ \ \   /\  d0np                     |
             | / / / / /   ||  @unb.ca        OR        |
             | \ \ \ \ \   ||  @jupiter.sun.csd.unb.ca  |
             | / / / / /   ||     Michael G. Gemmell    |
             | \ \ \ \ \   ||                           |
             | / / / / / ------    I hereby disclaim    |
             | \ \ \ \ \   ()     that my opinions are  |
             | / / / / /   ()    soley my own, I'm sure |
             |              nobody else would want them |
             --------------------------------------------



More information about the Comp.lang.c mailing list