[comp.lang.c] Structures

ADLER1%BRANDEIS.BITNET@wiscvm.wisc.EDU (07/15/87)

I am interested in finding a list of c compilers and their implementations
which also explains how the basic data types of c (char,int, etc) are
actually implemented. I think it would be healthy for such a list to
exist and be updated. I am for the moment particularly interested in the
following cases: gnu cc on SUN, original K&R c compiler on VAX/UNIX,
VAX C on VMS, the C compiler normally resident on SUN, ditto for VAX/UNIX
(probably the same as K&R: I don't know the history). I'm also interested
in a few for the IBM PC XT such as Optimizing C86 Compiler from Computer
Innovations and the DeSmet C compiler.

I could of course read the documentation for each one in the hope of
finding what I want to know. But it seems that a database of specifications
for various implementations of various c compilers would be a more general
and useful solution. If it doesn't exist, it seems that there must be enough
expertise on this network to create one overnight.

I welcome your comments and suggestions.

Sincerely,
Allan Adler
ADLER1@BRANDEIS.BITNET

minar@reed.bitnet (Nelson Minar,(???)) (03/01/90)

My copy of K&R 1 (I'll be getting 2 soon..) says

"There are only two things that can be done with a structure or a union:
name one of its members (by means of the . operator); or take its address
(by unary &).  Other operations, such as assigning from or to it or passing
it as a parameter, draw an error message.  In the future, it is expected
that these operations, but not necessarily others, will be allowed."

1] What does ansi say about this?

2] It seems that passing a structure-by-value is illegal under this rule.
Why this limitation in the original C?

3] My C compiler (Turbo C 2.0) doesn't mind passing a structure-by-value,
and it allows you to set two structures equal to each other, automatically
copying the elements. Is this standard behavior for most C compilers? Why
does Turbo C have an <optional> warning "Warning: structure passed by value"
if it is legal TC 2.0 code? Because it is potentially non-portable?

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

In article <14302@reed.UUCP> minar@reed.bitnet (Nelson Minar,(???)) writes:

| My copy of K&R 1 (I'll be getting 2 soon..) says
| 
| "There are only two things that can be done with a structure or a union:
| name one of its members (by means of the . operator); or take its address
| (by unary &).  Other operations, such as assigning from or to it or passing
| it as a parameter, draw an error message.  In the future, it is expected
| that these operations, but not necessarily others, will be allowed."
| 
| 1] What does ansi say about this?

ANSI requires that implementations be able to pass structures by
value, return them, and assigning structures.  Note that the maximum
size of a structure that you can pass is implementation defined.  For
example, unless they've changed the calling sequence recently, on a
VAX, the size of all of the arguments must be less than 512 longwords
(or bytes, I don't use vaxen).

| 2] It seems that passing a structure-by-value is illegal under this rule.
| Why this limitation in the original C?

Possibly because the PDP11 did not have a block move instruction, or
possibly Dennis Ritchie hadn't gotten around to it when K&R-I was
written.  The changes mentioned in #1, were made after K&R-I, but
before V7 was shipped outside of Bell Labs.  The same change added
enum's.

| 3] My C compiler (Turbo C 2.0) doesn't mind passing a structure-by-value,
| and it allows you to set two structures equal to each other, automatically
| copying the elements. Is this standard behavior for most C compilers? Why
| does Turbo C have an <optional> warning "Warning: structure passed by value"
| if it is legal TC 2.0 code? Because it is potentially non-portable?

The warning message is probably because the usage is not allowed by
K&R (though most compilers do support it).
--
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/03/90)

In article <14302@reed.UUCP> minar@reed.bitnet (Nelson Minar) writes:
>My copy of K&R 1 (I'll be getting 2 soon..) says
>"There are only two things that can be done with a structure or a union...

K&R1 was obsolete a decade ago, in this and some other minor respects.

>1] What does ansi say about this?

Structures can be assigned, passed, and returned, plus (with some limits)
initialized.

>2] It seems that passing a structure-by-value is illegal under this rule.
>Why this limitation in the original C?

Basically because C came out of the BCPL heritage, in which there was
really only one data type (the word) and all values were one word wide.
C departed from this pretty early for floating point, but bigger departures
were slow in coming.  Passing pointers to structs is usually more efficient
and met most early needs just as well.

>3] My C compiler (Turbo C 2.0) doesn't mind passing a structure-by-value,
>and it allows you to set two structures equal to each other, automatically
>copying the elements. Is this standard behavior for most C compilers? Why
>does Turbo C have an <optional> warning "Warning: structure passed by value"
>if it is legal TC 2.0 code? Because it is potentially non-portable?

It's standard behavior for modern compilers; the warning is probably
because it's not K&R1 conformant.
-- 
MSDOS, abbrev:  Maybe SomeDay |     Henry Spencer at U of Toronto Zoology
an Operating System.          | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

karl@haddock.ima.isc.com (Karl Heuer) (03/03/90)

In article <1990Mar2.171407.1194@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>In article <14302@reed.UUCP> minar@reed.bitnet (Nelson Minar) writes:
>>Why does Turbo C have an <optional> warning "Warning: structure passed by
>>value" if it is legal TC 2.0 code?  Because it is potentially non-portable?
>
>It's standard behavior for modern compilers; the warning is probably
>because it's not K&R1 conformant.

I think it's more likely that the warning exists simply to catch a common
mistake.  How often do you actually pass a struct by value on purpose,
compared to the number of times you accidentally forget to enreference it?

Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint

schaut@cat9.cs.wisc.edu (Rick Schaut) (03/04/90)

In article <14302@reed.UUCP> minar@reed.bitnet (Nelson Minar) writes:
| 
| My copy of K&R 1 (I'll be getting 2 soon..) says
| 
| "There are only two things that can be done with a structure or a union:
| name one of its members (by means of the . operator); or take its address
| (by unary &).  Other operations, such as assigning from or to it or passing
| it as a parameter, draw an error message.  In the future, it is expected
| that these operations, but not necessarily others, will be allowed."
| 
| 1] What does ansi say about this?

According to K&R 2, p129:

	The only legal operations on a structure are copying it or assigning
	to it as a unit, taking its address with &, and accessing its members.
	Copy and assignment include passing arguments to functions and
	returning values from functions as well.

| 2] It seems that passing a structure-by-value is illegal under this rule.
| Why this limitation in the original C?

Because C was originaly written for a DEC PDP11 which only had 64K of
ram.  Under those conditions, passing structures by value simply takes
up too much stack space.

| 3] My C compiler (Turbo C 2.0) doesn't mind passing a structure-by-value,
| and it allows you to set two structures equal to each other, automatically
| copying the elements. Is this standard behavior for most C compilers? Why
| does Turbo C have an <optional> warning "Warning: structure passed by value"
| if it is legal TC 2.0 code? Because it is potentially non-portable?

It's a warning because most people (including me) still try to restrict
themselves to passing only pointers to structures.  I'm not even sure why
you'd ever want to pass a structure by value.  Note that it is a _warning_,
not an _error_, message which means that the code isn't necessarily illegal,
only that it has a _potential_ error.

--
Rick (schaut@garfield.cs.wisc.edu)

"I'm a theory geek; we use Turing machines!"--Gary Lewandowski

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

In article <4390@daffy.cs.wisc.edu> schaut@cat9.cs.wisc.edu (Rick Schaut) writes:
>| 2] It seems that passing a structure-by-value is illegal under this rule.
>| Why this limitation in the original C?
>
>Because C was originaly written for a DEC PDP11 which only had 64K of
>ram.  Under those conditions, passing structures by value simply takes
>up too much stack space.

You'd be amazed at how many programs used to fit comfortably in 64KB before
people with financial interests in memory sales got hold of them...  The
first compiler with struct passing and returning was Dennis's pdp11 one,
which had many happy users for a long time.

>It's a warning because most people (including me) still try to restrict
>themselves to passing only pointers to structures.  I'm not even sure why
>you'd ever want to pass a structure by value...

The main motive for this, in my experience, is when you're essentially
building your own data types (complex numbers in sci/eng number-crunching,
two-dimensional coordinates in graphics, etc.) and want to treat them as
if they were ordinary values.  Passing pointers gets troublesome very
quickly in such situations.
-- 
MSDOS, abbrev:  Maybe SomeDay |     Henry Spencer at U of Toronto Zoology
an Operating System.          | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

eric@snark.uu.net (Eric S. Raymond) (03/06/90)

In <1990Mar5.183244.1013@utzoo.uucp> Henry Spencer wrote:
> The main motive for [structure-passing] is when you're essentially
> building your own data types (complex numbers in sci/eng number-crunching,
> two-dimensional coordinates in graphics, etc.) and want to treat them as
> if they were ordinary values.  Passing pointers gets troublesome very
> quickly in such situations.

As usual, Henry is correct. However, speaking as a partisan of ADT design
techniques who likes to build entire program systems as collections of live
data types, I avoid passing structs anyhow. Pointers are significantly faster
to hand around and cheaper to store.

Of course this is not a problem if your type is int size or less. A struct
consisting of two short ints (useful in graphics work, for example) is an
excellent candidate to be passed around by value rather than by address.
-- 
      Eric S. Raymond = eric@snark.uu.net    (mad mastermind of TMN-Netnews)

chris@mimsy.umd.edu (Chris Torek) (03/07/90)

In article <MEISSNER.90Mar1163541@curley.osf.org> meissner@osf.org
(Michael Meissner) writes:
>ANSI requires that implementations be able to pass structures by
>value, return them, and assigning structures.  Note that the maximum
>size of a structure that you can pass is implementation defined.  For
>example, unless they've changed the calling sequence recently, on a
>VAX, the size of all of the arguments must be less than 512 longwords
>(or bytes, I don't use vaxen).

This is true but misleading: the VAX can easily pass more than 255
longwords, but when it does so, the magic `pop this many longwords'
count (for the `ret' instruction) will be incorrect and the compiler
must compensate for this.  Some (non C) routines may also get confused
as to how many arguments were given.

>>[why did K&R-1 C not have structure valued arguments?]

>Possibly because the PDP11 did not have a block move instruction, or
>possibly Dennis Ritchie hadn't gotten around to it when K&R-I was
>written.

The latter seems the most likely explanation.  Things were added to the
orignal PDP-11 C compiler as they became useful; structure-valued
arguments were not often wanted.  This same C compiler did not have
a `long' data type in Version 6, for instance.  Fortunately, K&R 1st
edition was written after the `long' type was added.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris