digitalmars.D.learn - Array of array
- RenatoL (7/7) Jan 02 2012 auto r = new int[][5];
- Timon Gehr (5/12) Jan 02 2012 What would you expect the code to do?
- RenatoL (4/4) Jan 02 2012 Just curious... the answer of the compiler it's a bit unclear to
- Timon Gehr (8/12) Jan 02 2012 It does work. Why do you think it does not?
- Mafi (11/24) Jan 02 2012 Here we come to an interesting point I often thought of. How do you
- Timon Gehr (4/31) Jan 02 2012 Yes, but you can use (new T[][1]).ptr; to allocate T[] itself (and get a...
- Steven Schveighoffer (13/47) Jan 07 2012 Interesting trivia, the compiler actually transforms the following:
- bearophile (6/22) Jan 07 2012 What are the disadvantages caused by this?
- Steven Schveighoffer (8/28) Jan 07 2012 Wasted space. An appendable array requires extra space at the end of th...
- bearophile (5/14) Jan 07 2012 Thank you for your answers.
- Steven Schveighoffer (4/20) Jan 07 2012 http://d.puremagic.com/issues/show_bug.cgi?id=7243
- bearophile (4/5) Jan 07 2012 Thank you :-) I think in Bugzilla there is space for few performance-rel...
- RenatoL (7/7) Jan 02 2012 I have:
- Timon Gehr (3/10) Jan 02 2012 new int[][3] is an alternate form of new int[][](3); new int[][3]
- Matej Nanut (5/21) Jan 02 2012 I assume `int[][] sth;` does what RenatoL wants to accomplish by `auto s...
- Timon Gehr (45/61) Jan 02 2012 It is not the same thing.
- Manfred Nowak (4/6) Jan 02 2012 There is an error in the error message:
- Jonathan M Davis (6/12) Jan 02 2012 Um no. new is used for creating _dynamic_ arrays, not static arrays. Sta...
- Manfred Nowak (8/9) Jan 02 2012 Correct, my fault. I meant something like "statically initialized dynami...
auto r = new int[][5]; this is ok auto r = new int[][]; this is not ok Error: new can only create structs, dynamic arrays or class objects , not int[][]'s why?
Jan 02 2012
On 01/02/2012 11:04 PM, RenatoL wrote:auto r = new int[][5]; this is ok auto r = new int[][]; this is not ok Error: new can only create structs, dynamic arrays or class objects , not int[][]'s why?What would you expect the code to do? What you are trying to achieve is similar to: class Array(T){this(size_t length){...}} auto r = new Array!(Array!int); // error: missing constructor argument
Jan 02 2012
Just curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T. T[][] is a dynamic array of T[]. But this doesn't work. Why?
Jan 02 2012
On 01/02/2012 11:21 PM, RenatoL wrote:Just curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T. T[][] is a dynamic array of T[]. But this doesn't work. Why?It does work. Why do you think it does not? T[] a; // ok T[][] b; // ok auto c = new T[5]; // ok auto d = new T[][5]; // ok auto e = new T[]; // fail, nonsensical auto f = new T[][]; // fail, nonsensical
Jan 02 2012
Am 02.01.2012 23:33, schrieb Timon Gehr:On 01/02/2012 11:21 PM, RenatoL wrote:Here we come to an interesting point I often thought of. How do you allocate a T[] itself (so you get a T[]*) or a ClassType reference (so you get a ClassType*) on the heap (without casting)? As far as I know it's not possible with new. new T[n] is of type T[]. new T[]* is of type T[]**. new ClassType is of type ClassType. new ClassType* is of type ClassType**. Is this a Hole of new? MafiJust curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T. T[][] is a dynamic array of T[]. But this doesn't work. Why?It does work. Why do you think it does not? T[] a; // ok T[][] b; // ok auto c = new T[5]; // ok auto d = new T[][5]; // ok auto e = new T[]; // fail, nonsensical auto f = new T[][]; // fail, nonsensical
Jan 02 2012
On 01/03/2012 12:02 AM, Mafi wrote:Am 02.01.2012 23:33, schrieb Timon Gehr:Yes, but you can use (new T[][1]).ptr; to allocate T[] itself (and get a T[]*). Maybe new T[] should be interpreted as allocating a T[] and give back a T[]*.On 01/02/2012 11:21 PM, RenatoL wrote:Here we come to an interesting point I often thought of. How do you allocate a T[] itself (so you get a T[]*) or a ClassType reference (so you get a ClassType*) on the heap (without casting)? As far as I know it's not possible with new. new T[n] is of type T[]. new T[]* is of type T[]**. new ClassType is of type ClassType. new ClassType* is of type ClassType**. Is this a Hole of new? MafiJust curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T. T[][] is a dynamic array of T[]. But this doesn't work. Why?It does work. Why do you think it does not? T[] a; // ok T[][] b; // ok auto c = new T[5]; // ok auto d = new T[][5]; // ok auto e = new T[]; // fail, nonsensical auto f = new T[][]; // fail, nonsensical
Jan 02 2012
On Mon, 02 Jan 2012 18:30:52 -0500, Timon Gehr <timon.gehr gmx.ch> wrote:On 01/03/2012 12:02 AM, Mafi wrote:Interesting trivia, the compiler actually transforms the following: struct S { int x; } auto s = new S; into this: auto s = (new S[1]).ptr; Found that out when revamping the array allocation code. It's one thing that still bugs me because this means s will be initialized as an appendable array when it doesn't have to be. -SteveAm 02.01.2012 23:33, schrieb Timon Gehr:Yes, but you can use (new T[][1]).ptr; to allocate T[] itself (and get a T[]*). Maybe new T[] should be interpreted as allocating a T[] and give back a T[]*.On 01/02/2012 11:21 PM, RenatoL wrote:Here we come to an interesting point I often thought of. How do you allocate a T[] itself (so you get a T[]*) or a ClassType reference (so you get a ClassType*) on the heap (without casting)? As far as I know it's not possible with new. new T[n] is of type T[]. new T[]* is of type T[]**. new ClassType is of type ClassType. new ClassType* is of type ClassType**. Is this a Hole of new? MafiJust curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T. T[][] is a dynamic array of T[]. But this doesn't work. Why?It does work. Why do you think it does not? T[] a; // ok T[][] b; // ok auto c = new T[5]; // ok auto d = new T[][5]; // ok auto e = new T[]; // fail, nonsensical auto f = new T[][]; // fail, nonsensical
Jan 07 2012
Steven Schveighoffer:Interesting trivia, the compiler actually transforms the following: struct S { int x; } auto s = new S; into this: auto s = (new S[1]).ptr; Found that out when revamping the array allocation code. It's one thing that still bugs me because this means s will be initialized as an appendable array when it doesn't have to be.What are the disadvantages caused by this? If the disadvantages are significant is this improvement in Bugzilla as enhancement request? Struct heap allocations are important, they are a basic part of D programming. Bye, bearophile
Jan 07 2012
On Sat, 07 Jan 2012 13:29:25 -0500, bearophile <bearophileHUGS lycos.com> wrote:Steven Schveighoffer:Wasted space. An appendable array requires extra space at the end of the block to store the 'used' length.Interesting trivia, the compiler actually transforms the following: struct S { int x; } auto s = new S; into this: auto s = (new S[1]).ptr; Found that out when revamping the array allocation code. It's one thing that still bugs me because this means s will be initialized as an appendable array when it doesn't have to be.What are the disadvantages caused by this?If the disadvantages are significant is this improvement in Bugzilla as enhancement request?It's not in bugzilla, but it's strictly a performance thing, I don't think the current method causes any harm, it's just not as optimal as it could be. -Steve
Jan 07 2012
Steven Schveighoffer:Wasted space. An appendable array requires extra space at the end of the block to store the 'used' length.Thank you for your answers. This has to go in Bugzilla. You are more qualified than me to write it. But if you don't want to write it, I'll write it myself. Bye, bearophileIf the disadvantages are significant is this improvement in Bugzilla as enhancement request?It's not in bugzilla, but it's strictly a performance thing, I don't think the current method causes any harm, it's just not as optimal as it could be.
Jan 07 2012
On Sat, 07 Jan 2012 14:24:19 -0500, bearophile <bearophileHUGS lycos.com> wrote:Steven Schveighoffer:http://d.puremagic.com/issues/show_bug.cgi?id=7243 -SteveWasted space. An appendable array requires extra space at the end of the block to store the 'used' length.Thank you for your answers. This has to go in Bugzilla. You are more qualified than me to write it. But if you don't want to write it, I'll write it myself.If the disadvantages are significant is this improvement in Bugzillaasenhancement request?It's not in bugzilla, but it's strictly a performance thing, I don't think the current method causes any harm, it's just not as optimal as it could be.
Jan 07 2012
Steven Schveighoffer:http://d.puremagic.com/issues/show_bug.cgi?id=7243Thank you :-) I think in Bugzilla there is space for few performance-related enhancement requests too, if they are about very common/important parts of the language :-) Bye, bearophile
Jan 07 2012
I have: auto r = new int[][]; Error: new can only create structs, dynamic arrays or class objects , not int[][]'s while auto r = new int[][3]; is ok.
Jan 02 2012
On 01/03/2012 12:03 AM, RenatoL wrote:I have: auto r = new int[][]; Error: new can only create structs, dynamic arrays or class objects , not int[][]'s while auto r = new int[][3]; is ok.new int[][3] is an alternate form of new int[][](3); new int[][3] allocates an int[][] with 3 default-initialized elements.
Jan 02 2012
On 3 January 2012 00:27, Timon Gehr <timon.gehr gmx.ch> wrote:On 01/03/2012 12:03 AM, RenatoL wrote:I assume `int[][] sth;` does what RenatoL wants to accomplish by `auto sth = new int[][];`. I do however have a question: is `auto sth = new int[][5];` the same as `int[][5] sth;`? If not, what is the difference?I have: auto r = new int[][]; Error: new can only create structs, dynamic arrays or class objects , not int[][]'s while auto r = new int[][3]; is ok.new int[][3] is an alternate form of new int[][](3); new int[][3] allocates an int[][] with 3 default-initialized elements.
Jan 02 2012
On 01/03/2012 12:46 AM, Matej Nanut wrote:On 3 January 2012 00:27, Timon Gehr <timon.gehr gmx.ch <mailto:timon.gehr gmx.ch>> wrote: On 01/03/2012 12:03 AM, RenatoL wrote: I have: auto r = new int[][]; Error: new can only create structs, dynamic arrays or class objects , not int[][]'s while auto r = new int[][3]; is ok. new int[][3] is an alternate form of new int[][](3); new int[][3] allocates an int[][] with 3 default-initialized elements. I assume `int[][] sth;` does what RenatoL wants to accomplish by `auto sth = new int[][];`. I do however have a question: is `auto sth = new int[][5];` the same as `int[][5] sth;`? If not, what is the difference?It is not the same thing. First of all, lets get rid of int[][] and use int[] for further reasoning. int[] is a data structure with two members: 'ptr' and 'length'. 'ptr' is a pointer to the first element of the array, and 'length' indicates how many elements are in the array. int[] does not carry any data on its own: it is only a reference to the data. int[5] on the other hand is a data structure that contains 5 integers with the indices 0,1,2,3,4. |ptr|length| <- int[] |0|1|2|3|4| <- int[5] An int[5] can be sliced to get an int[]: void main(){ int[5] arr1=[0,1,2,3,4]; int[] arr2 = arr1[]; // arr2 is now a reference to arr1's data: assert(arr2.ptr is &arr1[0] && arr2.length == 5); // changing arr2 changes arr1: arr2[0] = 5; assert(arr1 == [5,1,2,3,4]); // assigning int[5] to int[5] copies the data int[5] arr3 = arr1; assert(arr2.ptr !is &arr1[0]); // therefore, changing arr3 lets arr1 intact arr3[0] = 0; assert(arr1 == [5,1,2,3,4]); } void main(){ int[] arr1; // creates an int[] on the stack with ptr=null and length=0 int[5] arr2; // creates in int[5] on the stack with arr2[i]=0 for i in 0,1,2,3,4 arr1 = arr2[]; // array slice lets arr1 reference the data of arr2 arr1 = new int[](5); // allocates an int[5] on the heap and lets arr1 reference the newly created data arr1 = new int[5]; // same as the above, syntax carried over from C++/Java } With this in mind, it is now possible to understand the difference between int[][5] sth and auto sth = new int[][5]: void main(){ int[][5] sth1; // creates an int[][5] on the stack (5 default-initialized int[]s) auto sth2 = new int[][5]; // creates an int[][5] on the heap and lets sth2 reference it. }
Jan 02 2012
RenatoL wrote:Error: new can only create structs, dynamic arrays or class objects, not int[][]'sThere is an error in the error message: new can only create _static_ arrays. -manfred
Jan 02 2012
On Monday, January 02, 2012 23:49:36 Manfred Nowak wrote:RenatoL wrote:Um no. new is used for creating _dynamic_ arrays, not static arrays. Static arrays normally go on the stack. int[5] staticArray; auto dynamicArray = new int[](5); - Jonathan M DavisError: new can only create structs, dynamic arrays or class objects, not int[][]'sThere is an error in the error message: new can only create _static_ arrays.
Jan 02 2012
Jonathan M Davis wrote:new is used for creating _dynamic_ arrays, not static arrays.Correct, my fault. I meant something like "statically initialized dynamic", because `new' currently needs an `uint' number to allocate some space for the elements of the outermost array. The posting shows, that it is correct not to initialize the length of a dynamic array by some number (even zero would be wrong). Despite of this: the errormessage is missing an important point. -manfred
Jan 02 2012