🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4083

Re: Function-prototype scope: Why?

#4083
From: Tanmoy Bhattacharya
Date: Fri, 22 Sep 1995 21:19
90 lines
4111 bytes
In article <43urgs$gkd@ae-sun01.alden.com>, sgr@ae-sun01.alden.com (Stan
Ryckman) writes: 
|> In article <43ugdt$ihe@newshost.lanl.gov>,
<snip>
|> >I think the statement is strictly true only if one makes the assumption
that 
|> >the linkers are not incredibly smart. All proofs I have seen are based on
|> >translation units containing incomplete struct pointers (which, by the way
|> >are not incomplete types): and in the absence of smart linkers, all struct
|>      ^^^^^^^^^^^^^^^^^^^^
|>      (That's why I'm using the term "pointer to incomplete struct")
|> >pointers need to be the same shape for things to work.
|> 
|> The following seems legal [is it? gcc likes it] but not handleable

It is implementation defined. All implementations I know of define it as
`correct'. 

|> by a linker however smart unless "shape" is known:
|> 	struct S;
|> 	struct cs { char c; };
|> 	struct is { int i; };
|> 	void bar(struct cs*);
|> 	void baz(struct is*);
|> 	void foo( int is_cs, struct S *inc)
|> 	{
|> 	    is_cs ? bar((struct cs *)inc) : baz((struct is *)inc);
|> 	}

A better example (I hope you will agree with me when you see it) would be

extern struct S *a;
struct T {int x;} *b;
struct cs; struct is;
void bar(struct cs*);
void baz(struct is*);
void foo(int is_cs)
{ int p[sizeof a - sizeof b + 1];
 is_cs == sizeof a ? bar((struct cs*)a) : baz((struct is*)a); }

in one translation unit; and

extern struct S *b;
struct T {double y;} *a;
void foo(int is_cs);
int main(void){ char q [sizeof a - 3]; foo(sizeof q); return 0; }

in another unit. 

Now the point is that the compiler has to _know_ the sizeof a and sizeof b
_at compile time_ to detect whether or not a constraint is violated (array
dimension has to be positive). (I am assuming that the implementation is not
dishonest by giving one diagnostic `The program may have errors.' on all
programs: correct or incorrect). On the other hand, the sizeof a that it
knows, must be the same as the sizeof a in the other unit: A little analysis
now proves that sizeof on all structure pointers _must_ be the same.

What about the shape? This is the part I was complaining about. All structure
pointers can be implemented as the following union

extern size_t __defaultdataseg;
union {struct {size_t offset;} x;
       struct {size_t segment, offset;} y;};

Pointers with external linkage could always be in the `big' form (or
alternatively, name mangling and a smart linker could splice in appropriate
converters). Function calls would be with the native format of each pointer.
With name mangling, a linker could put in converters when big --> small and
small --> big calls are made. This is what I was talking about.

|> 
|> In other words, if casting a (pointer to incomplete struct) to
|> (pointer to some other struct type) is legal, then more than linker
|> issues are involved, I think.

Yes: more than linker, but still doable.

|> 
|> (Of course, "proofs" derived from inferences from the standard
|> must assume arbitrarily smart linkers, at least in this group :-)

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: <43v98u$qcd@newshost.lanl.gov>
Path: arc1-ca.pugleaf.net!qcd.lanl.gov!tanmoy
References: <43cgmb$cec@news0.rain.rg.net> <43qod5$clf@ae-sun01.alden.com> <43s7k4$j2v@gatekeeper.atml.co.uk> <43ugdt$ihe@newshost.lanl.gov> <43urgs$gkd@ae-sun01.alden.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: Function-prototype scope: Why?
Date: 22 Sep 1995 21:19:26 GMT
Organization: Los Alamos National Laboratory
Distribution: world
Message-ID: <43v98u$qcd@newshost.lanl.gov>
References: <43cgmb$cec@news0.rain.rg.net> <43qod5$clf@ae-sun01.alden.com> <43s7k4$j2v@gatekeeper.atml.co.uk> <43ugdt$ihe@newshost.lanl.gov> <43urgs$gkd@ae-sun01.alden.com>
NNTP-Posting-Host: qcd.lanl.gov