Re: implicite deref on array element access? (indexing)

2011-02-25 Thread Jonathan M Davis
On Friday, February 25, 2011 07:15:52 spir wrote:
> Hello,
> 
> I thought it worked, just like implicite deref on (struct, class) member
> access. But I cannot have it work:
> 
>  auto a = [1,2,3];
>  auto pa = &a;
>  writeln((*pa)[2]);  // ok
>  writeln(pa[2]); // segfault
> 
> Denis

The _only_ time that dereferencing is done automatically in D is with the dot 
operator.

- Jonathan M Davis


Re: implicite deref on array element access? (indexing)

2011-02-25 Thread spir

On 02/25/2011 04:43 PM, Steven Schveighoffer wrote:

On Fri, 25 Feb 2011 10:15:52 -0500, spir  wrote:


Hello,

I thought it worked, just like implicite deref on (struct, class) member
access. But I cannot have it work:

auto a = [1,2,3];
auto pa = &a;
writeln((*pa)[2]); // ok
writeln(pa[2]); // segfault


Because indexing a pointer like ptr[n] is the equivalent of *(ptr + n).

This is how it is in C.

Fun fact, you can avoid array bounds checks (if you know the index is valid) by
doing arr.ptr[n]


All right! This is what I did not get. Thank you, Steve.
Too bad. Anyway, in the meanwhile I have opted for another approach. (FWIW, in 
Oberon implicite deref works on array indexing just like on struct [record] 
member access.)


Denis
--
_
vita es estrany
spir.wikidot.com



Re: implicite deref on array element access? (indexing)

2011-02-25 Thread Steven Schveighoffer
On Fri, 25 Feb 2011 10:58:49 -0500, Andrej Mitrovic  
 wrote:



On 2/25/11, Steven Schveighoffer  wrote:

Fun fact, you can avoid array bounds checks (if you know the index is
valid) by doing arr.ptr[n]


Can't you do the same with -noboundscheck ?


No, -noboundscheck stops bounds checking everywhere.  arr.ptr[n] stops  
bounds checking for that one statement.  It's a lot easier to prove that  
one time that bounds checking is not necessary than it is to prove that no  
bounds checking is necessary anywhere.


Plus, you can't always control the command line.

-Steve


Re: implicite deref on array element access? (indexing)

2011-02-25 Thread Andrej Mitrovic
On 2/25/11, Steven Schveighoffer  wrote:
> Fun fact, you can avoid array bounds checks (if you know the index is
> valid) by doing arr.ptr[n]

Can't you do the same with -noboundscheck ?


Re: implicite deref on array element access? (indexing)

2011-02-25 Thread Steven Schveighoffer

On Fri, 25 Feb 2011 10:15:52 -0500, spir  wrote:


Hello,

I thought it worked, just like implicite deref on (struct, class) member  
access. But I cannot have it work:


 auto a = [1,2,3];
 auto pa = &a;
 writeln((*pa)[2]);  // ok
 writeln(pa[2]); // segfault


Because indexing a pointer like ptr[n] is the equivalent of *(ptr + n).

This is how it is in C.

Fun fact, you can avoid array bounds checks (if you know the index is  
valid) by doing arr.ptr[n]


-Steve


Re: implicite deref on array element access? (indexing)

2011-02-25 Thread Jesse Phillips
spir Wrote:

> Hello,
> 
> I thought it worked, just like implicite deref on (struct, class) member 
> access. But I cannot have it work:
> 
>  auto a = [1,2,3];
>  auto pa = &a;
>  writeln((*pa)[2]);  // ok
>  writeln(pa[2]); // segfault

You aren't making a pointer to the data, only the array. I probably would have 
made the same mistake, but I think you want to use:

auto pa = a.ptr;

Don't know if there is a ptr property for array.


Re: implicite deref on array element access? (indexing)

2011-02-25 Thread Andrej Mitrovic
P.S. I got bitten by this when I was interfacing with C. Only in my
case I used multidimensional arrays. I just assumed they were the same
thing as in C, but I was wrong.


Re: implicite deref on array element access? (indexing)

2011-02-25 Thread Andrej Mitrovic
Actually that doesn't explain anything!

What I mean is, arrays in D are not the same as arrays in C.

auto a = [1, 2, 3];
auto pa = &a;

writeln(&a[0]);
writeln(&a[1]);
writeln(&a[2]);

writeln(&pa[0]);
writeln(&pa[1]);
writeln(&pa[2]);

9A2E40
9A2E44
9A2E48
12FE34
12FE3C
12FE44

Afaik D arrays have a length and then a pointer to the contents of the
array (someone correct me on this if I'm wrong?).


Re: implicite deref on array element access? (indexing)

2011-02-25 Thread Andrej Mitrovic
This should explain everything:

auto a = [1,2,3];
auto pa = &a;
writeln(&pa);
writeln(&pa+1);

Do the math!


implicite deref on array element access? (indexing)

2011-02-25 Thread spir

Hello,

I thought it worked, just like implicite deref on (struct, class) member 
access. But I cannot have it work:


auto a = [1,2,3];
auto pa = &a;
writeln((*pa)[2]);  // ok
writeln(pa[2]); // segfault

Denis
--
_
vita es estrany
spir.wikidot.com