www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - int[3][4]*

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
alright what's the deal?

void main () {
     alias int[3][4] fooz;
     int[3][4]* i = new fooz;
}


wiz.d(6): Error: new can only create structs, dynamic arrays or class 
objects, not int[3LU][4LU]'s
Sep 07 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Ellery Newcomer:

 alright what's the deal?
This is one of the "clean" ways to do it: void main () { static struct Mat { int[3][4] m; alias m this; } Mat* fooz = new Mat; fooz[1][3] = 5; } Bye, bearophile
Sep 07 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 09/08/2012 04:19 AM, bearophile wrote:
 Ellery Newcomer:

 alright what's the deal?
This is one of the "clean" ways to do it: void main () { static struct Mat { int[3][4] m; alias m this; } Mat* fooz = new Mat; fooz[1][3] = 5;
This may corrupt your heap.
 }


 Bye,
 bearophile
I prefer this: void main(){ alias int[3][4] fooz; int[3][4]* i = (new fooz[1]).ptr; } But I think the original example should just work.
Sep 07 2012
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Timon Gehr:

 This may corrupt your heap.
I usually don't put the alis this...
 I prefer this:

 void main(){
     alias int[3][4] fooz;
     int[3][4]* i = (new fooz[1]).ptr;
 }
This allocates past the size of the array, the information to append to the array of fooz. It's a very little amount of memory. Since some weeks, if you allocate a single struct that contains a single fixed size array, that memory is not allocated. Bye, bearophile
Sep 08 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/08/2012 01:21 PM, bearophile wrote:
 Timon Gehr:

 This may corrupt your heap.
I usually don't put the alis this...
 I prefer this:

 void main(){
     alias int[3][4] fooz;
     int[3][4]* i = (new fooz[1]).ptr;
 }
This allocates past the size of the array, the information to append to the array of fooz. It's a very little amount of memory. Since some weeks, if you allocate a single struct that contains a single fixed size array, that memory is not allocated. Bye, bearophile
Wasn't aware that this got fixed. Thanks!
Sep 08 2012
prev sibling parent Artur Skawina <art.08.09 gmail.com> writes:
On 09/08/12 05:27, Timon Gehr wrote:
 On 09/08/2012 04:19 AM, bearophile wrote:
 Ellery Newcomer:

 alright what's the deal?
This is one of the "clean" ways to do it: void main () { static struct Mat { int[3][4] m; alias m this; } Mat* fooz = new Mat; fooz[1][3] = 5;
This may corrupt your heap.
Yeah, this would be (the) one reason for introducing reference variables to the language; it's too easy right now to introduce such bugs (and if the code happens to compile it will just silently return bogus results). It's a bit late for such a change (and making it more backwards compatible by having refs that implicitly convert to pointers would be even more confusing while not solving the problem). Restricting certain operations on pointers-to-custom-types might work, but also carries a cost (eg indexing pointers to such types wouldn't be possible unless via a (temp) slice) as there probably already is code out there that accesses arrays of indexable-structs via pointers... artur
Sep 08 2012