Signed word lengths and indexes

bearophile bearophileHUGS at lycos.com
Fri Jun 18 09:30:17 PDT 2010


Michel Fortin:
> Bypassing bound checks is as easy as appending ".ptr":
> 
> 	staticArray.ptr[10]; // no bound check
> 
> Make an alias to the static array's ptr property if you prefer not to 
> have to write .ptr all the time.

If you try to compile this:

import std.c.stdlib: malloc;
struct Foo {
  int x;
  int[0] a;
}
void main() {
  enum N = 20;
  Foo* f = cast(Foo*)malloc(Foo.sizeof + N * typeof(Foo.a[0]).sizeof);
  f.a.ptr[10] = 5;
}

You receive:
prog.d(9): Error: null dereference in function _Dmain

As I have said, you have to use operator overloading of the struct and some near-ugly code that uses the offsetof. I don't like this a lot.

Bye,
bearophile


More information about the Digitalmars-d mailing list