home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
- From: torek@horse.ee.lbl.gov (Chris Torek)
- Newsgroups: comp.lang.c
- Subject: Re: How does the Address-of Operator (&) really work?
- Date: 12 Nov 1992 13:31:42 GMT
- Organization: Lawrence Berkeley Laboratory, Berkeley
- Lines: 69
- Distribution: world
- Message-ID: <27403@dog.ee.lbl.gov>
- References: <1ds9htINNd81@agate.berkeley.edu>
- Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
- NNTP-Posting-Host: 128.3.112.15
-
- In article <1ds9htINNd81@agate.berkeley.edu> achoi@soda.berkeley.edu
- (Andrew Choi) writes:
- > I was told that the address-of operator & (along with sizeof)
- >is done at compilation [time]
-
- This is not strictly correct. Many systems will compute addresses
- during compilation, rather than at runtime, but there is no such
- guarantee in the language. Furthermore, addresses of automatic
- objects do not even exist at compile time:
-
- void pointless(void) {
- int i, *j = &i;
- ...
- }
-
- The value `&i' is not a compile-time constant.
-
- >therefore, it becomes legal to do the followings:
- >
- > &(((struct S *) NULL)->field)
-
- This also is false. It usually works, and is how many (if not most)
- implementations actually implement the ANSI `offsetof' macro, but it is
- not guaranteed to work, or to return anything useful. (This is *why*
- ANSI *has* an offsetof macro.)
-
- >However, what I don't understand is how it works under multiple
- >files. Considered the followings:
- >
- > /* file "foo.c" */
- > char *a = "This is a test";
- >
- > /* file "bar.c" */
- > char **b = &a;
- >
- >When the 2 files, "bar.c" and "foo.c" are separately compiled,
- >what does the compiler put for "&a", since it does not know where
- >the definition for "a" is?
-
- The compiler must emit some sort of directive to the rest of the
- compilation system---which may be called the `linker' on your machine
- ---telling it that it has to initialize `b' before calling main().
- This initialization is typically performed at link time, although
- again there is no such guarantee in the C language.
-
- Remember that the ANSI standard says only that there is a *system* that
- acts as specified by the standard. It does not say how the system is
- built, or how it is to do what it must. It says only *what happens*.
-
- The actual mechanics on traditional systems, at least for this case,
- are that the `object files' are in a special format (e.g., a bit
- stream, a sequence of records, a set of sections, or whatever) that
- includes a `symbol table' and `relocation information'. The relocation
- information says things like:
-
- In file bar.o, at offset +104 in the read/write data section,
- insert the value of the global symbol `_a'.
-
- The actual encoding varies wildly from one system to another. Any
- system that implements the ANSI C standard must provide *some*
- mechanism by which a static pointer variable can be set to point to a
- named static object plus an integer constant offset, or to a named
- function (without an offset), but the standard (and this newsgroup)
- will not say how *your* system does this. Your system's particular
- needs are unique to your system, and you should find them described
- in documents (and newsgroups) also specific to your system.
- --
- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
- Berkeley, CA Domain: torek@ee.lbl.gov
-