[comp.lang.c] COMMONS in C

rothen@unix.cis.pitt.edu (Seth B Rothenberg) (03/19/90)

I am trying to convert a fortran code-generator into a
C code-generator.  I think I have worked out a scheme for
commons,  but I was recently told something that casts some
doubt on my idea.

I was thinking that a common could be converted into a structure
e.g.,
COMMON TEST /A/B/C/
INTEGER A,B,C

becomes

struct st_com_TEST {
	int A,B,C;
};

extern struct st_com_TEST *com_TEST;

I have actually tried this on the vax and it worked.
But someone told me recently that the order of the variables in 
memory is not always the same as the order of the variables in the 
structure.   Is this true?

Based on the way structures can be initialized, I think the
person I spoke with is mistaken.
I would appreciate any comments.
Thanks
Seth Rothenberg
rothen@unix.cis.pitt.edu

news@awdprime.UUCP (USENET News) (03/20/90)

In article <22944@unix.cis.pitt.edu> rothen@unix.cis.pittsburgh.edu (Seth B Rothenberg) writes:
>I have actually tried this on the vax and it worked.
>But someone told me recently that the order of the variables in 
>memory is not always the same as the order of the variables in the 
>structure.   Is this true?

I don't understand why the rest of the code would depend on the order
of the variables in memory.  Are you sure that the order matters to
the rest of the generated code???

It would be legal for a compiler to change the in memory order of
structure elements (though I don't think anyone does this), as it is
also legal for compilers to insert unused bytes to pad elements to fit
onto memory boundries (really nasty if you write out structures on
machine A and try and read them on machine B, a big no no).

-- sanders                          The 11th commandment: "Thou shalt use lint"
For every message of the day, a new improved message will arise to overcome it.
Reply-To: cs.utexas.edu!ibmaus!auschs!sanders.austin.ibm.com!sanders     (ugh!)

henry@utzoo.uucp (Henry Spencer) (03/20/90)

In article <22944@unix.cis.pitt.edu> rothen@unix.cis.pittsburgh.edu (Seth B Rothenberg) writes:
>... someone told me recently that the order of the variables in 
>memory is not always the same as the order of the variables in the 
>structure.   Is this true?

Both K&R1 and ANSI C say the order must be preserved.  There are probably
compilers that don't do this, however.

>Based on the way structures can be initialized, I think the
>person I spoke with is mistaken.

Structure initialization has no bearing on the question, since the compiler
need only rearrange the order of elements in the initializer to match.
-- 
MSDOS, abbrev:  Maybe SomeDay |     Henry Spencer at U of Toronto Zoology
an Operating System.          | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

dwg@bpdsun1.uucp (David W. Glessner) (03/21/90)

In article <1990Mar19.213645.1962@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
                              in structures
>Both K&R1 and ANSI C say the order ^ must be preserved.

Are any guarantees made about the order (i.e. addresses) of external
variables?
--
David	quintro!bpdsun1!dwg@lll-winken.llnl.gov
	uunet!tiamat!quintro!bpdsun1!dwg

meissner@osf.org (Michael Meissner) (03/22/90)

In article <1990Mar21.031726.18192@bpdsun1.uucp> dwg@bpdsun1.uucp
(David W. Glessner) writes:

| In article <1990Mar19.213645.1962@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
|                               in structures
| >Both K&R1 and ANSI C say the order ^ must be preserved.
| 
| Are any guarantees made about the order (i.e. addresses) of external
| variables?

Nope.  The Data General MV linker sorts global variables by alignment,
so that the items with the largest alignment come first (the C
compiler also sorts static data by alignment and size).  The VMS
linker sorts global variables alphabetically.  The System V.3 linker
allows the use of a runtime script to determine where things go.
--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA

Catproof is an oxymoron, Childproof is nearly so

henry@utzoo.uucp (Henry Spencer) (03/22/90)

In article <1990Mar21.031726.18192@bpdsun1.uucp> quintro!bpdsun1!dwg@lll-winken.llnl.gov (David W. Glessner) writes:
>                              in structures
>>Both K&R1 and ANSI C say the order ^ must be preserved.
>
>Are any guarantees made about the order (i.e. addresses) of external
>variables?

None whatsoever.
-- 
Never recompute what you      |     Henry Spencer at U of Toronto Zoology
can precompute.               | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

wallis@labc.dec.com (Barry L. Wallis) (03/22/90)

In article <1990Mar21.031726.18192@bpdsun1.uucp>, dwg@bpdsun1.uucp (David W. Glessner) writes...
>In article <1990Mar19.213645.1962@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>                              in structures
>>Both K&R1 and ANSI C say the order ^ must be preserved.
> 
>Are any guarantees made about the order (i.e. addresses) of external
>variables?

The order of variables in a structure statement *must* be preserved (UNION
wouldn't be much use otherwise). However variables which are independent from
each other have no such guarantee. The following is from the _VAX C Run-Time
Library Reference Manual (V3.1)_:

"The C language does not gurantee any memory order for the variables in a
declaration. For example,

	int a, b, c;

"The VMS Linker (linker) usually places VAX C extern variables in program
sections (psects) of the same name as the variable. The linker then
alphabetically links the psects by name. If you are porting a C program from
another operating system to a VMS system, you may find that the order of items
in the program has been allocated differently in virtual memory. This causes
existing programs with hidden bugs to fail"

---
Barry L. Wallis			USENET: wallis@labc.dec.com
Database Consultant		Prodigy (don't laugh): DNMX41A
U.S. DECtp Resource Center	DECUServe: EISNER::WALLIS (not on the net yet)
Los Angeles, CA			"No one voted for me, I represent myself"
---