Article View: comp.std.c
Article #4239Re: Variable length struct hack
From: Tanmoy Bhattacharya
Date: Wed, 18 Oct 1995 19:50
Date: Wed, 18 Oct 1995 19:50
73 lines
2973 bytes
2973 bytes
In article <463egk$jbb@nntp.Stanford.EDU>, becker@Xenon.Stanford.EDU (Denizen
of the Deep) writes:
|> In reference to the practise of declaring a struct as:
|>
|> struct blah {
|> int num;
|> char data[1];
|> };
|>
|> From the FAQ 9.7:
|>
|> To be on the safe side, it may be preferable to declare the
|> variable-size element very large, rather than very small; in the case
|> of the above example:
|>
|> ...
|> char name[MAXSIZE];
|> ...
|>
|> where MAXSIZE is larger than any name which will be stored. (The trick
|> so modified is said to be in conformance with the Standard.)
|>
|> I seem to remember hearing recently that even this modification is
|> not actually in conformance with the Standard, but I can't remember
|> why. Can anyone enlighten me?
The questions are
1) Is the following portable?
struct blah {
int num;
char data[10000];
} *ptr = malloc (sizeof (struct blah) - 9000);
Point is `Is malloc required to return space properly aligned for even an
object which does not fit into the space provided?' i.e. is the result of
malloc aligned for struct blah in this case.
The standard says that the return value of malloc is suitably aligned so
that it can be assigned to a pointer of any kind. I guess they meant `without
information loss'.
2) Is the following portable?
ptr->data[0];
Point is that the struct *ptr, and the array ptr->data does not actually
exist (C has no concept of partial objects). So, do subexpression
which occur in value contexts have to actually exist (*ptr is probably not in
a value context, but ptr->data certainly is!) for the full expression to be
valid?
I do not know the answer to this, but I believe it does: the standard never
makes a distinction between expressions and subexpressions in a value
context.
|>
|> If it isn't, is there any way at all to portably achieve the desired
|> behavior (i.e. all the data in one contiguous block)?
|>
No. If this does not work, there is no portable way when the size is not
known at compile time.
Cheers
Tanmoy
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
Technical Details
Message-ID:
<463lpa$qgo@newshost.lanl.gov>Path:
arc1-ca.pugleaf.net!qcd.lanl.gov!tanmoyReferences:
<463egk$jbb@nntp.Stanford.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!qcd.lanl.gov!tanmoy From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya) Newsgroups: comp.std.c Subject: Re: Variable length struct hack Date: 18 Oct 1995 19:50:02 GMT Organization: Los Alamos National Laboratory Distribution: world Message-ID: <463lpa$qgo@newshost.lanl.gov> References: <463egk$jbb@nntp.Stanford.EDU> NNTP-Posting-Host: qcd.lanl.gov