Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: a question about speed difference that I do not understand

Author: Miguel A. Ballicora

Date: 12:12:33 12/05/01

Go up one level in this thread


On December 05, 2001 at 14:23:13, Dieter Buerssner wrote:

>>It is not what I have in my C  book but maybe my C book is not correct(it
>>assumes also that int is 2 bytes when I know that int is 4 bytes)
>
>It is implemetation defined. Most modern implemetation use 4 bytes.
>
>>It is claimed there that if you define
>>int my_array[10];
>>then it means that you get 10 varaibles of 2 bytes and I understand that
>>my_array[10] is not defined.
>
>Your book is correct. A better formulation may be, that you get 10 int
>variables. Accessing my_array[10] is definetly an error.

Just to complete Dieter's comment: accessing my_array[10] could led to
terrible errors as he said. However, the address of my_array[10] can be
perfectly used. You just can't peek what's in it.

for instance:
int a, *p;

*p = &my_array[10];  /* is correct code */

a = my_array[10];    /* incorrect */
a = *p;              /* incorrect */
a = *(p-1);          /* correct again */

This might sound a little picky or stupid but it becomes extremely
important if you work with pointers. If you scan an array with a pointer,
the last value of the pointer is legal as long as you do not use it!

for (p = &my_array[0]; p < &my_array[10]; p++) {
  do_something_with (*p);
}

here, p points to my_array[10], this is legal but you cannot use *p

Regards,
Miguel






>
>Regards,
>Dieter



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.