Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Compiler Question

Author: Sune Fischer

Date: 05:50:01 01/19/02

Go up one level in this thread


On January 18, 2002 at 21:04:24, David Rasmussen wrote:
>But if I have declared an array statically in C (in my case it is C++), like
>this:
>
>int a[10];
>
>int i = ...;
>
>a[i] = 42;
>
>or
>
>struct S
>{
>  int a[10];
>};
>
>S s;
>
>s.a[i] = 42;
>
>then a is a constant pointer, not just a pointer. And the compiler could very
>well assume (at least if I told it to), that array accesses via a cannot be
>outside bounds. You can write all sorts of code that would make this not work,
>but using such a feature would be the programmers responsibility.
>
>/David

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.






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.