Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Steve Thompson <stevet810@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Compatibility question |
| Date | 2012-10-23 12:28 +0000 |
| Organization | Friends of the Galactic Collective |
| Message-ID | <45fj92.pJU.yDawq@gmail.com> (permalink) |
| References | (5 earlier) <lnfw56fl74.fsf@nuthaus.mib.org> <qRFu8I.lcH.7XEwQ@gmail.com> <0.5ef6d4d4944842791bc8.20121022210639BST.87pq4a9sq8.fsf@bsb.me.uk> <ap54B1.hSk.bor8X@gmail.com> <0.3c5d812f7189744b9ad3.20121023033332BST.87d309apdv.fsf@bsb.me.uk> |
On Tue, Oct 23, 2012 at 03:33:32AM +0100, Ben Bacarisse wrote:
> Steve Thompson <stevet810@gmail.com> writes:
>
> > On Mon, Oct 22, 2012 at 09:06:39PM +0100, Ben Bacarisse wrote:
> >> Steve Thompson <stevet810@gmail.com> writes:
> >>
> >> > On Mon, Oct 22, 2012 at 10:52:47AM -0700, Keith Thompson wrote:
> >> >> Steve Thompson <stevet810@gmail.com> writes:
> >> >> [...]
> >> >> > No annoyance taken, although now that I think of it it does show an
> >> >> > instance of overloaded semantics, which can be confusing. When I was
> >> >> > less experienced with C, I was confused by the fact that variables
> >> >> > defined as structures were not pointers:
> >> >> >
> >> >> > struct point {
> >> >> > int x, y, z;
> >> >> > };
> >> >> >
> >> >> > struct point a;
> >> >> >
> >> >> >
> >> >> > One cannot reference 'a' (as such) in an expression even though it is
> >> >> > a pointer in the implementation because C allows one to use structures
> >> >> > as arguments to a function. I'd never do that in my own code, but
> >> >> > some people may like the fact that they can pass complex data
> >> >> > structures around as if they were simple variables. I think it wastes
> >> >> > resources, but of course that is less of an issue today than it was
> >> >> > when C was first developed.
> >> >>
> >> >> I'm not sure what you mean when you say `a` "is a pointer in
> >> >> the implementation", especially after you mention "the fact that
> >> >> variables defined as structures were not pointers".
> >> >
> >> > Meaning that under the hood in the C compiler, 'a' is really a
> >> > pointer. The logic of the compiler causes it to use the structure
> >> > data as specified in the standard, but conceptually 'a' is still a
> >> > pointer to the data in the specific structure identified by 'a'. I
> >> > found it very confusing when I was learning C.
> >>
> >> I am not surprised. I would have found it totally baffling when I was
> >> learning C. I find it baffling even now, having learnt C!
> >>
> >> Whatever goes on under the hood (and I'd dispute that 'a' is "really a
> >> pointer") it's much easier to learn a language in its own terms, at
> >> least as far as that's possible.
> >
> > Well, no. 'a' is never a pointer as such in the terms of the language
> > syntax, but its treatment by the compiler internally is nominally
> > equivalent to they way it handles a pointer, with the exception that
> > the language grammar forbids its use in the program text as a pointer.
>
> I've been around Usenet long enough to recognise the signs. You are now
> 100% committed to this point of view and I don't think there's much
> point in continuing to debate it.
It's not a big deal; I'm not even sure there's a good reason to
discuss the difference between program objects and their instantiation
in an implementation, which are not defined by the standard.
> > In a simplistic sense, all variables are pointers to a segment of
> > program data, modifiable or otherwise. In the real world, variable
> > values may move around in and from RAM to CPU registers and back while
> > being identified as a single consistent entity. This helps to
> > illustrate the difference between a language and its implementation.
> >
> >> In C terms, 'a' names an object and 'a' is a (modifiable) lvalue
> >> expression, just as if it had been declared to be an int. trying to
> >> reconcile this with some model that says it's a pointer (when it isn't
> >> one) is bound to be confusing.
> >
> > I was never happy with the term lvalue or rvalue. My brain wants to
> > think in terms of pointers, variables, and values. Structures defined
> > as automatic variables don't fit all that neatly in that
> > classification.
>
> Then surely that's a sign that your "pointers, variables, and values"
> classification is unsuitable?
Or the lvalue/rvalue terminology is ugly. I have no problem
understanding structures as automatic variables. My quibble is with
their semantics.
Hypothetically, C might have been designed differently:
struct point {
int x, y, z;
};
struct point a, b;
a.x = 1;
a.y = 2;
a.z = 3;
With two distinct assignment operators, one might get a copied
structure, or a pointer to the original.
Copy:
b = a;
By reference:
b := a;
Someone else wrote a message yesterday suggesting that some languages
have this form of assignment, and indeed with this scheme there is no
ambiguity as to what is really going on. The difference between C and
this arrangement is in the definition of what 'a' is. Of course, in
this modified C, we have to decide what to do when passing 'a' or 'b'
to a function, and if I were designing this language for real I might
decide to pass simple types by value, and structures by reference.
I'm not sure which is better in practice. Quite apart from my
personal programming preference, the language semantics of C probably
make more sense, despite being a little confusing. A language with
the ':=' operator as above could easily do away with pointer types,
which is an interesting thing to consider. Without pointers, the '&'
operator could be reused to identify when a variable is being passed
by value or by reference, and thereby do away with the second ':='
assignment operator:
Copy:
a = b;
By reference:
a = &b;
In this paradigm variables become somewhat like inodes with a
reference count supporting hard links, at least in the hypothetical
language implementation.
Obviously this isn't C; I'm just thinking out loud about how things
might be done differently. I would never give up pointers, as they
are the single language feature that allows C to be used to write
operating systems and device drivers, and which allow one to perform
various tricks. One of my favorite is with an arena allocator for
fixed-sized objects. The free list is a pointer to the free 'object'
in the array, into which I store a small structure:
struct free_list {
int size;
int next;
};
'size' is the size (in 'objects') of the free block, and 'next'
is an offset into the arena where the next free chunk is. Allocations
are quick because all that is done is to decrement size by one, and
return the offset of the object. Let's say the header for such an
arena is defined as follows:
struct arena {
unsigned char *p;
int o_size;
struct free_list *f;
};
struct arena a;
int o;
an allocation might look something like this:
if(a->f) {
o = (((unsigned char)a->f - a->p) / a->o_size) + (a->f->size--);
} else {
return(-1);
}
if(!a->f->size) {
if(a->f->next != -1)
a->f = (struct free_list *)((unsigned char *)p + (a->f->next * a->o_size))
else
a->f = NULL;
}
That returns an effective index into a->p for an array of objects of
size a->o_size. There would be an access macro to work out the actual
address of the object. The scheme can also be modified to return
pointers which can be assigned directly to an arbitrary pointer type.
The following could be encapsulated in a macro to hide the ugly
details from the application program text, or the allocation function
could simply be declared to return a void * type as with malloc().
struct foo *o;
o = (void *)(((unsigned char *) a->f) + (a->f->size-- * a->o_size));
Either one is super fast, and on machines with out a FPU, it's
still quick when o_size is a power of two. The free operation is
slightly more complicated since I have to walk the free list to find
a free chunk adjacent to the free block, or insert one in the list.
Fragmentation can slow things down quite a lot, but allocations are
always fast, and there is no waste as there is with the standard
malloc. Coalescing free-list nodes automatically occurs during the
free operation. The free function trivially detects a bad pointer, as
well as objects which are already free.
This is why I like C.
Regards,
Steve Thompson
--
GOTO's are like guns. Safety is entirely contingent on the user.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-20 03:08 +0000
Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-20 16:20 +1300
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-20 11:50 +0100
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-20 11:41 -0700
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-21 23:04 +0000
Re: Compatibility question Barry Schwarz <schwarzb@dqel.com> - 2012-10-21 20:27 -0700
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-20 04:01 +0000
Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-21 09:26 +1300
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-21 22:54 +0000
Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-22 12:29 +1300
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-21 23:50 +0000
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 02:27 +0100
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 02:28 +0000
Re: Compatibility question Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-22 11:18 -0400
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 16:19 +0000
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 17:37 +0100
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 17:40 +0100
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 17:30 +0100
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 17:23 +0000
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-22 10:52 -0700
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 18:42 +0000
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 21:06 +0100
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 22:52 +0000
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-23 03:33 +0100
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-23 12:28 +0000
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 16:45 +0100
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-23 17:53 +0000
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-23 12:41 -0700
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 21:28 +0100
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-23 20:57 +0100
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-23 12:26 -0700
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-23 20:39 +0000
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-23 15:53 -0700
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-24 00:57 +0100
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-23 20:06 -0700
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-24 11:18 +0100
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 12:04 -0700
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-24 23:10 +0100
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 16:58 -0700
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-25 01:46 +0100
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 19:40 -0700
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-24 20:20 +0000
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 14:38 -0700
Re: Compatibility question James Kuyper <jameskuyper@verizon.net> - 2012-10-24 17:55 -0400
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 16:29 -0700
Re: Compatibility question Greg Martin <greg@softsprocket.com> - 2012-10-24 15:21 -0700
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-26 03:30 +0000
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-26 13:02 +0100
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-27 00:04 +0000
Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-27 20:15 +0100
Re: Compatibility question Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-10-27 16:36 -0600
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-28 17:36 +0000
Re: Compatibility question Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-10-27 22:42 +0300
Re: Compatibility question Greg Martin <greg@softsprocket.com> - 2012-10-27 14:30 -0700
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-27 22:57 +0100
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-27 17:04 -0700
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-27 17:14 -0700
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-27 18:04 -0700
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 16:46 -0700
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-25 01:18 +0000
Re: Compatibility question Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-10-27 21:08 +0300
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-28 17:38 +0000
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 00:38 +0100
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-22 16:50 -0700
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-23 12:33 +0000
Re: Compatibility question Öö Tiib <ootiib@hot.ee> - 2012-10-22 14:23 -0700
Re: Compatibility question James Kuyper <jameskuyper@verizon.net> - 2012-10-22 17:26 -0400
Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-23 10:55 +1300
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 23:27 +0000
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-22 22:44 +0100
Re: Compatibility question Les Cargill <lcargill99@comcast.com> - 2012-10-22 17:15 -0500
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 00:13 +0100
Re: Compatibility question James Kuyper <jameskuyper@verizon.net> - 2012-10-22 18:35 -0400
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 00:14 +0100
Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 23:42 +0000
Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-23 13:35 +1300
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-22 17:01 -0700
Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 01:47 +0100
Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-21 19:27 -0700
csiph-web