Jump to page: 1 24  
Page
Thread overview
why static array can be reassigned with new allocation?
Oct 14, 2022
mw
Oct 14, 2022
jfondren
Oct 15, 2022
rassoc
Oct 15, 2022
Nick Treleaven
Oct 15, 2022
Tejas
Oct 15, 2022
Timon Gehr
Oct 15, 2022
mw
Oct 15, 2022
Tejas
Oct 15, 2022
rassoc
Oct 15, 2022
mw
Oct 15, 2022
rassoc
Oct 15, 2022
mw
Oct 15, 2022
mw
Oct 15, 2022
rassoc
Oct 15, 2022
mw
Oct 16, 2022
Paul Backus
Oct 16, 2022
mw
Oct 16, 2022
Nick Treleaven
Oct 16, 2022
mw
Oct 16, 2022
Mike Parker
Oct 16, 2022
mw
Oct 16, 2022
Paul Backus
Oct 16, 2022
mw
Oct 16, 2022
Paul Backus
Oct 15, 2022
mw
Oct 14, 2022
Ali Çehreli
Oct 15, 2022
matheus
Oct 15, 2022
Nick Treleaven
Oct 16, 2022
Per Nordlöw
Oct 16, 2022
mw
Oct 16, 2022
Per Nordlöw
Oct 15, 2022
Nick Treleaven
Oct 15, 2022
Nick Treleaven
October 14, 2022

Hi,

import std.stdio;

void main() {
	int[3] arr;
	writeln(arr);
	arr = new int[5];
	writeln(arr);
}


$ dmd static_array.d
$ ./static_array
[0, 0, 0]
core.exception.RangeError@static_array.d(6): Range violation
----------------
??:? _d_arrayboundsp [0x55e4830afcb5]
??:? _Dmain [0x55e48308f957]


$ ldc2 static_array.d
$ ./static_array
[0, 0, 0]
Illegal instruction (core dumped)

Why this code can be compiled and causing runtime error? it should be caught at compile time!

October 14, 2022

On Friday, 14 October 2022 at 18:57:44 UTC, mw wrote:

>

Why this code can be compiled and causing runtime error? it should be caught at compile time!

import std.stdio;

void main() {
    int[3] arr;
    writeln(arr.ptr);
    arr = new int[3];
    writeln(arr.ptr);
    writeln(typeid(arr));
}

output (e.g.):

7FFD5EBF1530
7FFD5EBF1530
int[3]

It's not a reassignment of arr, but a copying of the contents of the dynamic array, and it crashes with an oversized array as the lengths don't match, same as if you did

    int[] other = new int[5];
    foreach (i; 0 .. other.length)
        arr[i] = other[i];
October 14, 2022
On 10/14/22 11:57, mw wrote:

>      arr = new int[5];

That syntax is confusing. Although it looks like a static array, it's a dynamic array of length 5.

> Why this code can be compiled and causing runtime error? it should be
> caught at compile time!

The compiler does not perform detailed analysis of all code. Since that 5 could be a runtime expression, the compiler does not check it at compile time.

Ali

October 15, 2022
On Friday, 14 October 2022 at 20:19:20 UTC, Ali Çehreli wrote:
> ...
> The compiler does not perform detailed analysis of all code. Since that 5 could be a runtime expression, the compiler does not check it at compile time.
> ...

Yes but in this case it's known during the compiling time (Literal 5), couldn't this be checked and prevented?

Matheus.
October 15, 2022
On 10/14/22 21:46, jfondren via Digitalmars-d wrote:
> and it crashes with an oversized array as the lengths don't match

Try compiling with -release for extra fun, or not.
October 15, 2022

On Saturday, 15 October 2022 at 10:31:16 UTC, matheus wrote:

>

On Friday, 14 October 2022 at 20:19:20 UTC, Ali Çehreli wrote:

>

...
The compiler does not perform detailed analysis of all code. Since that 5 could be a runtime expression, the compiler does not check it at compile time.
...

Yes but in this case it's known during the compiling time (Literal 5), couldn't this be checked and prevented?

It would be better to error on assigning a new array expression to a static array, there's no reason to allow that. A static array can be assigned a single element arr = 0 instead. Although ideally there would be some syntactical way of making it clear when assigning a slice to a static array, perhaps requiring [] at the end unless it is already a SliceExpression or an array literal. Then the syntax would tell you if the right hand side had a compile-time known length not (and hence you can rely on a compile-time error if lengths don't match).

October 15, 2022

On Friday, 14 October 2022 at 20:19:20 UTC, Ali Çehreli wrote:

>

On 10/14/22 11:57, mw wrote:

>
 arr = new int[5];

That syntax is confusing. Although it looks like a static array, it's a dynamic array of length 5.

I recently updated the docs to prefer the new E[](length) form instead:

>

It is preferred to use the Type(ArgumentList) form when allocating dynamic arrays instead, as it is more general.

>

Note: It is not possible to allocate a static array directly with new (only by using a type alias).

https://dlang.org/spec/expression.html#NewExpression

At some point the T[length] form should be deprecated. Perhaps we could have a compiler switch -strict that deprecates a set of things, e.g. struct postblit. This would be used for anything which needs a long deprecation period and to avoid annoying people by adding new deprecations spread out over a number of releases. Instead they can update their code using the -strict check once in a while.

October 15, 2022

On Friday, 14 October 2022 at 18:57:44 UTC, mw wrote:

>

$ dmd static_array.d
$ ./static_array
[0, 0, 0]
core.exception.RangeError@static_array.d(6): Range violation

??:? _d_arrayboundsp [0x55e4830afcb5]
??:? _Dmain [0x55e48308f957]

$ ldc2 static_array.d
$ ./static_array
[0, 0, 0]
Illegal instruction (core dumped)

Looks like an LDC bug.

October 15, 2022
On Saturday, 15 October 2022 at 11:29:00 UTC, rassoc wrote:
> On 10/14/22 21:46, jfondren via Digitalmars-d wrote:
>> and it crashes with an oversized array as the lengths don't match
>
> Try compiling with -release for extra fun, or not.

Use `@safe` then you still get bounds checks with `-release`. (Or don't use `-release`).
October 15, 2022

On Saturday, 15 October 2022 at 11:29:00 UTC, rassoc wrote:

>

On 10/14/22 21:46, jfondren via Digitalmars-d wrote:

>

and it crashes with an oversized array as the lengths don't match

Try compiling with -release for extra fun, or not.

It printed the arrays

[0, 0, 0]
[0, 0, 0]

... lol did it cache the results of the previous call? Somebody please explain this, is this what Timon meant by undefined behaviour getting introduced in programs whose asserts get removed from the code?

« First   ‹ Prev
1 2 3 4