Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.misc > #2207

Re: A good subset of C

From "Rod Pemberton" <dont_use_email@xnothavet.cqm>
Newsgroups comp.lang.misc
Subject Re: A good subset of C
Date 2014-05-06 06:48 -0400
Organization Aioe.org NNTP Server
Message-ID <op.xffpmfaf6zenlw@localhost> (permalink)
References <lk7flm$m95$1@dont-email.me> <lk8qjv$ejd$1@dont-email.me> <op.xfer8g1j6zenlw@localhost> <lk97lp$bkq$1@dont-email.me>

Show all headers | View raw


On Mon, 05 May 2014 19:42:51 -0400, James Harris  
<james.harris.1@gmail.com> wrote:
> "Rod Pemberton" <dont_use_email@xnothavet.cqm> wrote in message
> news:op.xfer8g1j6zenlw@localhost...
>> On Mon, 05 May 2014 15:59:26 -0400, James Harris
>> <james.harris.1@gmail.com> wrote:

>>> * Structs
>>
>> Yes.
>>
>> No typedefs?  They don't allocate space.  I.e., good for overlaying
>> data structures onto "arrays" of char, e.g., databases, records,
>> filesystems, struct nodes in a linked list, etc.
>
> Don't forget a typedef is just an alias for something that already  
> exists.
>

No, it's not for something that already exists.  Something must be declared
for it to "exist" as an object in C.  A typedef provides a reference to a
object type, which may be allocated or unallocated.  It acts as a language
element, a new type, an extension of C.  The typedef itself never exists as
an object.  I.e., it has no storage for data.   When used by itself, it
provides naming and addressing.  When used in a declaration, it will  
provide
naming, addressing, and allocation for the object defined in the  
declaration
according to the object used to define the typedef.


1) first example

/* The structA structure has been declared as a struct here */
/* The structA structure has space allocated for it here */
/* The layout of structA is defined here */
/* There is no typedef here */
struct structA {
   char c;
   int d;
};

/* Now structures structB and structC have been declared */
/* Both structB and structC have structA's layout */
/* Both structB and structC have space allocated for them */
   struct structA structB, structC;

/* This is a pointer to a struct of structA's layout */
/* The pointer hasn't been set to point to anything */
/* A struct has been allocated above to which it can be set */
   struct structA *tptr1;

2) second example

/* No structure has been declared here */
/* No structure has space allocated for it here */
/* The typedef represents the struct layout */
/* No space is allocated for the typedef */
/* tdef is the name of the typedef */
typedef struct {
   char c;
   int d;
} tdef;

/* Now structures struct1 and struct2 have been declared by typedef */
/* Both struct1 and struct2 have space allocated for them */
/* Both struct1 and struct2 have the struct layout of the typedef'd  
struct: tdef */
   tdef struct1, struct2;

/* This is a pointer to a struct of the typedef'd struct's layout */
/* The pointer hasn't been set to point to anything */
/* No struct has been allocated for which the pointer can point to */
/* The space for the pointer to point to is allocated via some other  
method */
   tdef *tptr2;


3) third example - you can combine them ...

/* structA has space allocated.  tdef1 and tdef2 do not. */
/* one struct can be defined, but multiple typedefs ... */
typedef struct structA {
   char c;
   int d;
} tdef1, tdef2;

   struct structA structB, structC; /* space is allocated for structs */
   struct structA *tptr1; /* pointer to struct with structA's layout */
   tdef1 struct1, struct2; /* space is allocated for structs*/
   tdef2 *tptr2; /* pointer to struct with structA's layout */

Isn't that lovely?  :-)

In the third example, you could say that tdef1 and tdef2 represent
the syntax 'struct structA', but you can't say that about the second
example since the struct is unnamed and C doesn't support unnamed
objects (or didn't, C99 might ...).

> Consider
>
>   struct Tag { ... };
>
> No typedef but, here, the struct needs no space allocation.
> It is just a definition and Tag is just a name.
>

Does the '...' represent C's ellipsis or stuff you didn't want
to type out?

Was this a struct prototype or an incomplete struct definition?

Once an incomplete struct definition is completed, or a full
structure is defined.  Space is allocated for that struct.

>>> * Choice of where to place variables: stack, block, module
>>
>> Why?
>>
>> (That kind of defeats the purpose of C.  Use Forth for that.)
>
> Consider
>
>   int a;
>   void fn() {
>     int b;
>     static int c;
>     for (int d;;) {
>     }
>     for (int e;;) {
>     }
>   }
>
> Here the intention is that A has module scope and storage, B has stack
> storage, C has module storage but function scope, and D has block scope  
> and
> probably some form of stack storage but would not have to compete with  
> E. D
> and E could occupy the same stack space.

Yes, but except for "A [with] module scope", nowhere do I, nor you,
need to know that.  It's convenient to know that "B has stack storage"
(typically).  In all cases, the compiler can relocate the storage from
memory, to register, to stack, vice-versa, either-or, whatever, and back,
space permitting, i.e., no structs will fit in a register.

I.e., while these have a typical way they're implemented in C, the
compiler can optimize or change that as needed.  Not dealing with this
issue directly allows the compiler to do the "best thing".  Specifically,
the fact C can generate code for a variable no matter where it's stored
or moved is one of the better ideas behind C, IMO.

Compare with Forth, where you can't do anything (almost nothing) to
their variables.  Forth doesn't have any (only one) defined operation
which works on variables.  They could easily, but don't.  To do things
the "Forth way", you must move them to the stack, manipulate the value
on the stack, perhaps with other values on the stack at the same time,
juggle them a bit, then store the decide result back into the variable.
All of that is done manually, by the programmer...


Rod Pemberton

Back to comp.lang.misc | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-05 08:47 +0100
  Re: A good subset of C "BartC" <bc@freeuk.com> - 2014-05-05 10:56 +0100
    Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-05 19:41 +0100
      Re: A good subset of C "BartC" <bc@freeuk.com> - 2014-05-05 21:08 +0100
        Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-05 21:58 +0100
          Re: A good subset of C "BartC" <bc@freeuk.com> - 2014-05-05 22:55 +0100
            Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-06 00:01 +0100
              Re: A good subset of C "BartC" <bc@freeuk.com> - 2014-05-06 00:44 +0100
          Re: A good subset of C "BartC" <bc@freeuk.com> - 2014-05-06 09:45 +0100
  Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-05 07:33 -0400
    Re: A good subset of C "BartC" <bc@freeuk.com> - 2014-05-05 19:14 +0100
    Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-05 20:12 +0100
      Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-05 18:57 -0400
        Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-07 11:55 +0100
          Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-07 19:04 -0400
            Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-08 10:14 +0100
              Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-08 07:35 -0400
                Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-08 16:22 +0100
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-09 16:45 -0400
                Re: A good subset of C "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-10 09:04 +0200
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-10 16:30 -0400
                Re: A good subset of C "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-11 11:59 +0200
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-11 11:06 -0400
                Re: A good subset of C Kaz Kylheku <kaz@kylheku.com> - 2014-05-12 00:26 +0000
                Re: A good subset of C "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-12 09:54 +0200
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-12 20:01 -0400
                Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-14 17:45 +0100
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-14 18:11 -0400
                Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-14 23:54 +0100
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-14 19:43 -0400
                Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-17 18:00 +0100
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-17 16:00 -0400
                Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-17 22:17 +0100
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-17 19:58 -0400
                Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-18 07:59 +0100
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-18 13:10 -0400
                Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-19 00:54 +0100
                Re: A good subset of C Andy Walker <news@cuboid.co.uk> - 2014-05-15 01:31 +0100
                Re: A good subset of C BGB <cr88192@hotmail.com> - 2014-05-14 23:43 -0500
                Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-17 18:38 +0100
                Re: A good subset of C Ian Collins <ian-news@hotmail.com> - 2014-05-25 11:04 +1200
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-24 20:36 -0400
                Re: A good subset of C Ian Collins <ian-news@hotmail.com> - 2014-05-25 12:53 +1200
                Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-24 21:34 -0400
                Re: A good subset of C Ian Collins <ian-news@hotmail.com> - 2014-05-25 14:11 +1200
                Re: A good subset of C Kaz Kylheku <kaz@kylheku.com> - 2014-05-25 06:47 +0000
                Re: A good subset of C Kaz Kylheku <kaz@kylheku.com> - 2014-05-25 06:43 +0000
  Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-05 20:59 +0100
    Re: A good subset of C "BartC" <bc@freeuk.com> - 2014-05-05 22:28 +0100
      Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-05 18:57 -0400
        Re: A good subset of C "BartC" <bc@freeuk.com> - 2014-05-06 00:26 +0100
      Re: A good subset of C George Neuner <gneuner2@comcast.net> - 2014-05-06 14:21 -0400
        Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-07 15:43 +0100
          Re: A good subset of C George Neuner <gneuner2@comcast.net> - 2014-05-07 16:03 -0400
            Re: A good subset of C "BartC" <bc@freeuk.com> - 2014-05-07 21:31 +0100
          Re: A good subset of C BGB <cr88192@hotmail.com> - 2014-05-07 15:41 -0500
    Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-05 18:46 -0400
      Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-06 00:42 +0100
        Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-06 06:48 -0400
          Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-07 15:40 +0100
            Re: A good subset of C "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-08 06:50 -0400
              Re: A good subset of C "James Harris" <james.harris.1@gmail.com> - 2014-05-08 15:43 +0100
                Re: A good subset of C George Neuner <gneuner2@comcast.net> - 2014-05-08 14:41 -0400
  Re: A good subset of C BGB <cr88192@hotmail.com> - 2014-05-05 19:08 -0500

csiph-web