home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17047 < prev    next >
Encoding:
Text File  |  1992-11-23  |  1.5 KB  |  52 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!jlnance
  3. From: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
  4. Subject: Re: Is this ANSI?
  5. Message-ID: <1992Nov23.180808.19157@ncsu.edu>
  6. Originator: jlnance@chow2.ece.ncsu.edu
  7. Lines: 38
  8. Sender: news@ncsu.edu (USENET News System)
  9. Reply-To: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
  10. Organization: North Carolina State University, Project Eos
  11. References: <9eRguB1w165w@quest.UUCP> <24238@alice.att.com> <By0vu2.7AF@unx.sas.com> <24262@alice.att.com> <By6HMs.I4r@unx.sas.com>
  12. Date: Mon, 23 Nov 1992 18:08:08 GMT
  13.  
  14.  
  15. In article <By6HMs.I4r@unx.sas.com>, sasghm@theseus.unx.sas.com (Gary Merrill) writes:
  16. |> 
  17. |> |> 
  18. |> |> > I think "does not allow that technique" is a little strong.  What you
  19. |> |> > *can* do is
  20. |> |> 
  21. |> |>     struct FM
  22. |> |>     {
  23. |> |>        short data_len;
  24. |> |>        char  data[1];
  25. |> |>         };
  26. |> |> 
  27. |> |>     struct FM *fmptr;
  28. |> |> 
  29. |> |>     fmptr = malloc( sizeof(struct FM) + ??? );
  30. |> |> 
  31.  
  32. I asked my C mentor about this once, and he pointed out that the compiler is
  33. free to store the members of the structure in any order.  Thus the compiler 
  34. could store data[1] in memory before it stores data_len.  Then if you wrote
  35. to data[1] you would clobber data_len.  I got around this by something like
  36. this:
  37.  
  38. struct FM {
  39.      short datalen;
  40.      char  *data;
  41.      }
  42.  
  43. struct FM *fmptr;
  44.  
  45. fmptr = malloc(sizeof(struct FM) + ??? + 1);  /* +1 only if you need null term*/
  46. fmptr->data = (char*) (fmptr+1);
  47.  
  48. This will work regardless of the ordering of the structure members.
  49.  
  50.  
  51. Jim Nance
  52.