Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct that apparently defines no instances in Unix v6

Tags:

c

unix

kernel

I'm going through the code of Unix version 6 with the Lion's book. One of the header files (param.h, can be accessed here) defines the following structs:

/*struct to access integers*/

/*single integer */
struct { int integ; };

/*in bytes*/
struct { char lobyte; char hibyte; };

These structures don't seem to define any instance, nor are they named so they can be used later. Does anybody know what is their use?

Thanks

like image 969
theprole Avatar asked Aug 29 '10 10:08

theprole


2 Answers

If someone included the whole file in a union declaration, it would allow them to access the different parts.

It would be something like:


  union{
   #include <param.h>
  } myparam;

  myparam.integ = 0xDEAD;
  assert(myparam.lobyte == 0xAD)
  assert(myparam.hibyte == 0xDE)

(Depends on endianness of architecture...)

So having looked around a bit, it seems that in old versions of C, you wouldn't have needed to declare the union ; there was only one namespace for all struct/union members that just translated into a byte offset that you could use on any variable. The best mention of this I could find is here : http://docs.sun.com/source/806-3567/compat.html Describing pre-ISO Sun C:

Allows struct, union, and arithmetic types using member selection operators ('.', '->') to work on members of other struct(s) or unions.
like image 156
rjw Avatar answered Oct 18 '22 03:10

rjw


Back in those days, the members of structures all shared the same namespace, not one namespace per structure. Consequently, each element of a structure had to have a unique name across all structures, or the same element had to appear with the same type at the same offset in every structure in which it appeared. Quite how that was used with these, I'm not sure, but I suspect you could do:

int x;

x.lobyte = 1;
x.hibyte = 2;

Or something analogous to that.

See also:

  • http://www.cs.bell-labs.com/who/dmr/chist.html
  • http://www.cs.bell-labs.com/who/dmr/primevalC.html

(Neither of those seems to answer this question, though.)

like image 1
Jonathan Leffler Avatar answered Oct 18 '22 02:10

Jonathan Leffler