🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4249

Re: Variable length struct hack

#4249
From: Tanmoy Bhattacharya
Date: Thu, 19 Oct 1995 15:21
67 lines
3380 bytes
In article <46449u$nb2@rdsunx.crd.ge.com>, volpe@bart.crd.ge.com (Christopher
R. Volpe) writes: 
<snip>
|> >struct blah {
|> >  int num;
|> >  char data[10000];
|> >} *ptr = malloc (sizeof (struct blah) - 9000);
|> 
|> Practically speaking? Probably. Strictly Conforming? Probably not.

The original poster to whom I was responding had specifically asked whether
this was completely portable.

|> 
|> >
|> >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.
|> 
|> First question: probably not. Second question: yes.
|> If you malloc, say, 3 bytes, malloc is not required to provide alignment
for a 
|> 4-byte object, say an int. Since the above malloc request will typically

It is funny. Last time this question came up, someone posted that the
committee believes that malloc result is always aligned for everything! Will
that person please repost? (after all, with responses unavailable
electronically, someone has to do the public service of speaking up every odd
time that this comes up :-)

(I did not then understand the logic that the committee followed: presumably
the statement which said that it is assignable to any pointer whatsoever.
Assigning an unaligned pointer can result in an invalid pointer, which can
result in undefined behaviour: so, this _could_ be interpreted to mean that
the pointer is aligned. I do not think the logic is convincing, but it may
have been the logic that was followed.

Do the record of responses contain only the `response' or also the logic by
which the resonse was reached?)

Am I misremembering completely?

|> evaluate to about 1004 bytes (10000 for "data" + 4 for "num" - 9000),
malloc 
|> has to presume that you might eventually use that storage for an array of
251 
|> ints.

But can a struct not have stricter alignment than its first member? So, the
result of malloc certainly has to be aligned for an int, but is it aligned
for struct blah? Again, I am not asking about `practically', but what is
guaranteed. 

(If it helps in thinking about it, change the `int num' to `char num' and
9000 to 9999. Assume further that one is interested in the subset of
conforming implementations which puts no padding between the fields, but puts
trailing padding to align all structs to 8-byte boundaries.)

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: <465qd1$k90@newshost.lanl.gov>
Path: arc1-ca.pugleaf.net!qcd.lanl.gov!tanmoy
References: <463egk$jbb@nntp.Stanford.EDU> <463lpa$qgo@newshost.lanl.gov> <46449u$nb2@rdsunx.crd.ge.com>

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: 19 Oct 1995 15:21:05 GMT
Organization: Los Alamos National Laboratory
Distribution: world
Message-ID: <465qd1$k90@newshost.lanl.gov>
References: <463egk$jbb@nntp.Stanford.EDU> <463lpa$qgo@newshost.lanl.gov> <46449u$nb2@rdsunx.crd.ge.com>
NNTP-Posting-Host: qcd.lanl.gov