Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Compiler Question

Author: David Rasmussen

Date: 07:54:48 01/19/02

Go up one level in this thread


On January 19, 2002 at 08:50:01, Sune Fischer wrote:

>
>I was told once by some linux-nerds that 'a' is not a pointer in this case.
>http://www.eskimo.com/~scs/C-faq/q6.2.html
>
>I said, "why not, you can use it just like a pointer?"
>And they went berserk;)
>
>--An array type describes a contiguously allocated nonempty set of
>objects with a particular member object type, called the element type.
>Array types are characterized by their element type and by the number of
>elements in the array. An array type is said to be derived from its
>element type, and if its element type is T, the array type is sometimes
>called array of T. The construction of an array type from an element
>type is called array type derivation.
>
>--A pointer type may be derived from a function type, an object type,
>or an incomplete type, called the referenced type. A pointer type
>describes an object whose value provides a reference to an entity of the
>referenced type. A pointer type derived from the referenced type T is
>sometimes called pointer to T. The construction of a pointer type
>from a referenced type is called pointer type derivation.
>
>And they constructed assembler code (I didn't understand the big deal then, and
>I still don't today ;) All I know now is that I will never call 'a' a pointer
>again.
>
>(sorry, the comments are in danish)
>
>int main() {
>    char buf[4];
>    char *buf2 = buf;
>
>    buf[0] = 1;
>    buf2[0] = 1;
>}
>
>. og tilgår noget hukommelse via dem. Hvordan ser koden så ud?
>
>Når man tilgår arrayen:
>
>  buf[0] = 1;
> 80483ec:       c6 45 fc 01             movb   $0x1,0xfffffffc(%ebp)
>
>Bliver der flyttet en byte direkte til arrayen hvis adresse (i forhold
>til stakken) vi kender. Der er ingen pointer involveret her.
>
>
>Men når vil tilgår dataene via pointeren:
>
>    buf2[0] = 1;
> 80483f0:       8b 45 f8                mov    0xfffffff8(%ebp),%eax
> 80483f3:       c6 00 01                movb   $0x1,(%eax)
>
>bliver pointerens værdi først hentet ind i et register og adresseret
>videre derfra.

For my point in this case (the compiler checking the bounds), it doesn't matter
whether you call it a pointer or not. But I'm pretty sure that C/C++ standard
and Stroustrups book semantically defines such an array as a constant pointer.
At any rate, I have never seen an example that didn't fit that definition.

How gcc generates code has nothing to do with the language itself. One could
make the point that there is nothing in assembler that is a pointer. A pointer
is a C/C++ thing, which on many platforms (but not all) is implemented as an
adress value. In the above example, the same address space is adressed, but in
different ways. It is a compiler optimization question.

/David



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.