home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!ferkel.ucsb.edu!taco!jlnance
- From: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
- Subject: Re: Is this ANSI?
- Message-ID: <1992Nov23.180808.19157@ncsu.edu>
- Originator: jlnance@chow2.ece.ncsu.edu
- Lines: 38
- Sender: news@ncsu.edu (USENET News System)
- Reply-To: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
- Organization: North Carolina State University, Project Eos
- References: <9eRguB1w165w@quest.UUCP> <24238@alice.att.com> <By0vu2.7AF@unx.sas.com> <24262@alice.att.com> <By6HMs.I4r@unx.sas.com>
- Date: Mon, 23 Nov 1992 18:08:08 GMT
-
-
- In article <By6HMs.I4r@unx.sas.com>, sasghm@theseus.unx.sas.com (Gary Merrill) writes:
- |>
- |> |>
- |> |> > I think "does not allow that technique" is a little strong. What you
- |> |> > *can* do is
- |> |>
- |> |> struct FM
- |> |> {
- |> |> short data_len;
- |> |> char data[1];
- |> |> };
- |> |>
- |> |> struct FM *fmptr;
- |> |>
- |> |> fmptr = malloc( sizeof(struct FM) + ??? );
- |> |>
-
- I asked my C mentor about this once, and he pointed out that the compiler is
- free to store the members of the structure in any order. Thus the compiler
- could store data[1] in memory before it stores data_len. Then if you wrote
- to data[1] you would clobber data_len. I got around this by something like
- this:
-
- struct FM {
- short datalen;
- char *data;
- }
-
- struct FM *fmptr;
-
- fmptr = malloc(sizeof(struct FM) + ??? + 1); /* +1 only if you need null term*/
- fmptr->data = (char*) (fmptr+1);
-
- This will work regardless of the ordering of the structure members.
-
-
- Jim Nance
-