digitalmars.D - typedef & new
- join (6/6) Jun 30 2004 Hello.
- Hauke Duden (7/13) Jun 30 2004 int[] is a reference to an integer array. The REFERENCE does not have to...
- Norbert Nemec (8/14) Jun 30 2004 ---------
- join (9/23) Jun 30 2004 OK, people... BUT:
- Ben Hinkle (11/40) Jun 30 2004 the same error happens if you take out the typedef and just do:
- join (8/15) Jun 30 2004 But here:
- Ivan Senji (7/25) Jun 30 2004 You cannot new a rectangular array an that is what vint is.
- Regan Heath (23/42) Jun 30 2004 The trouble stems from int[] and int[256] being slightly different thing...
- join (12/60) Jun 30 2004 I've got some bug in me... :)
- Regan Heath (14/91) Jun 30 2004 I found this too.. I think it might be a bug.
- Walter (7/18) Jun 30 2004 Static arrays cannot be created using new. Static arrays have types with...
- Regan Heath (13/37) Jun 30 2004 I think what he was trying to do was this
- Ivan Senji (6/29) Jul 01 2004 I hope this will change in the future (dmd 1.3?)
- Norbert Nemec (6/16) Jul 01 2004 It is the very definition of a static array that you know the dimensions...
- Ivan Senji (7/23) Jul 01 2004 with
- C. Sauls (13/15) Jun 30 2004 I believe that error message is ambiguous. It should specifically say
- join (35/45) Jul 01 2004 //and...
Hello. One question it is. Why I cannot do something like this: typedef int[] vint; vint vector = new vint; // [error] ?
Jun 30 2004
join wrote:Hello. One question it is. Why I cannot do something like this: typedef int[] vint; vint vector = new vint; // [error]int[] is a reference to an integer array. The REFERENCE does not have to be explicitly allocated. If you want to allocate the ARRAY you must specify a size, otherwise it wouldn't be possible to determine the amount of memory it uses. For example: int[] vint = new int[10]; Hauke
Jun 30 2004
join wrote:Hello. One question it is. Why I cannot do something like this: typedef int[] vint; vint vector = new vint; // [error]--------- typedef int[] vint; vint* vector = new vint; // legal but rather pointless: you are allocating // the space for one reference on the heap vint vector = cast(vint)(new int[13]); // more useful but awkward vint vector; vector.length = 13; // This does the magic ---------
Jun 30 2004
OK, people... BUT: typedef int[] vint; vint* vector = new vint; // [ERROR]: new can only create structs, arrays or class objects, not vint's I made some type by _typedef_. Now I’m going to allocate it somehow as other types. How can I do it? Or can I do it anyway? :-\ In article <cbunr7$ilh$1 digitaldaemon.com>, Norbert Nemec says...join wrote:Hello. One question it is. Why I cannot do something like this: typedef int[] vint; vint vector = new vint; // [error]--------- typedef int[] vint; vint* vector = new vint; // legal but rather pointless: you are allocating // the space for one reference on the heap vint vector = cast(vint)(new int[13]); // more useful but awkward vint vector; vector.length = 13; // This does the magic ---------
Jun 30 2004
the same error happens if you take out the typedef and just do: int main() { int* vector = new int[]; return 0; } so you see the key is you need to specify how big an array to allocate - not just that you want to allocate an array. "join" <join_member pathlink.com> wrote in message news:cbv2ja$120u$1 digitaldaemon.com...OK, people... BUT: typedef int[] vint; vint* vector = new vint; // [ERROR]: new can only create structs, arraysorclass objects, not vint's I made some type by _typedef_. Now I'm going to allocate it somehow as other types. How can I do it? Or can I do it anyway? :-\ In article <cbunr7$ilh$1 digitaldaemon.com>, Norbert Nemec says...allocatingjoin wrote:Hello. One question it is. Why I cannot do something like this: typedef int[] vint; vint vector = new vint; // [error]--------- typedef int[] vint; vint* vector = new vint; // legal but rather pointless: you are// the space for one reference on the heap vint vector = cast(vint)(new int[13]); // more useful but awkward vint vector; vector.length = 13; // This does the magic ---------
Jun 30 2004
But here: int main ( char[][] args ) { int[] vector1 = new int[256]; // done typedef int[256] vint; // specify the dimention vint* vector2 = new vint; // [error]: what's wrong now? return 0; } In article <cbv4bl$14om$1 digitaldaemon.com>, Ben Hinkle says...the same error happens if you take out the typedef and just do: int main() { int* vector = new int[]; return 0; } so you see the key is you need to specify how big an array to allocate - not just that you want to allocate an array.
Jun 30 2004
"join" <join_member pathlink.com> wrote in message news:cbv93g$1bjg$1 digitaldaemon.com...But here: int main ( char[][] args ) { int[] vector1 = new int[256]; // done typedef int[256] vint; // specify the dimention vint* vector2 = new vint; // [error]: what's wrong now?You cannot new a rectangular array an that is what vint is. You can do: vint array; vint *vector2 = &array;return 0; } In article <cbv4bl$14om$1 digitaldaemon.com>, Ben Hinkle says...notthe same error happens if you take out the typedef and just do: int main() { int* vector = new int[]; return 0; } so you see the key is you need to specify how big an array to allocate -just that you want to allocate an array.
Jun 30 2004
On Wed, 30 Jun 2004 20:50:56 +0000 (UTC), join <join_member pathlink.com> wrote:But here: int main ( char[][] args ) { int[] vector1 = new int[256]; // done typedef int[256] vint; // specify the dimention vint* vector2 = new vint; // [error]: what's wrong now? return 0; }The trouble stems from int[] and int[256] being slightly different things, and the syntax used with each beig different also. int[] is a dynamic array, it can be resized etc. int[256] is a static array, it cannot be resized. However, when you say 'new int[256]' you create a dynamic array (int[]) NOT a static array (int[256]). You have two choices when allocating an array... 1. use a dynamic array like so: int[] a = new int[256]; 2. use a static array like so: int[256] a; both the above allocate an array of 256 int's. Given that, it does not seem possible to use a typedef as you have described, but, it also seems pointless to me. You can go: typedef int[256] vint; vint a; if you want vint to mean "a static array of length 256". ReganIn article <cbv4bl$14om$1 digitaldaemon.com>, Ben Hinkle says...-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/the same error happens if you take out the typedef and just do: int main() { int* vector = new int[]; return 0; } so you see the key is you need to specify how big an array to allocate - not just that you want to allocate an array.
Jun 30 2004
I've got some bug in me... :) int main (char[][] args) { ubyte[256] vec1 = new ubyte[256]; alias ubyte[256] vec256; vec256* vec2 = new vec256; // [error]: new can only create structs, _arrays_ or class objects, not ubyte[256]'s // Is ubyte[256] not array? 8-) // Can we make new array at runtime? :D return 0; } In article <opsafftaxo5a2sq9 digitalmars.com>, Regan Heath says...On Wed, 30 Jun 2004 20:50:56 +0000 (UTC), join <join_member pathlink.com> wrote:But here: int main ( char[][] args ) { int[] vector1 = new int[256]; // done typedef int[256] vint; // specify the dimention vint* vector2 = new vint; // [error]: what's wrong now? return 0; }The trouble stems from int[] and int[256] being slightly different things, and the syntax used with each beig different also. int[] is a dynamic array, it can be resized etc. int[256] is a static array, it cannot be resized. However, when you say 'new int[256]' you create a dynamic array (int[]) NOT a static array (int[256]). You have two choices when allocating an array... 1. use a dynamic array like so: int[] a = new int[256]; 2. use a static array like so: int[256] a; both the above allocate an array of 256 int's. Given that, it does not seem possible to use a typedef as you have described, but, it also seems pointless to me. You can go: typedef int[256] vint; vint a; if you want vint to mean "a static array of length 256". ReganIn article <cbv4bl$14om$1 digitaldaemon.com>, Ben Hinkle says...-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/the same error happens if you take out the typedef and just do: int main() { int* vector = new int[]; return 0; } so you see the key is you need to specify how big an array to allocate - not just that you want to allocate an array.
Jun 30 2004
On Wed, 30 Jun 2004 22:26:15 +0000 (UTC), join <join_member pathlink.com> wrote:I've got some bug in me... :)I found this too.. I think it might be a bug. Both alias and typedef give similar tho slightly different errors. However as Ivan mentions, your statement is still slightly wrong, you cannot go alias ubyte[256] vec256; vec256* vec2 = new vec256; new vec256 returns an array reference, not a pointer, you have to go alias ubyte[256] vec256; ubyte[] tmp = new vec256; vec256* vec2 = &tmp;int main (char[][] args) { ubyte[256] vec1 = new ubyte[256]; alias ubyte[256] vec256; vec256* vec2 = new vec256; // [error]: new can only create structs, _arrays_ or class objects, not ubyte[256]'s // Is ubyte[256] not array? 8-) // Can we make new array at runtime? :D return 0; } In article <opsafftaxo5a2sq9 digitalmars.com>, Regan Heath says...-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/On Wed, 30 Jun 2004 20:50:56 +0000 (UTC), join <join_member pathlink.com> wrote:But here: int main ( char[][] args ) { int[] vector1 = new int[256]; // done typedef int[256] vint; // specify the dimention vint* vector2 = new vint; // [error]: what's wrong now? return 0; }The trouble stems from int[] and int[256] being slightly different things, and the syntax used with each beig different also. int[] is a dynamic array, it can be resized etc. int[256] is a static array, it cannot be resized. However, when you say 'new int[256]' you create a dynamic array (int[]) NOT a static array (int[256]). You have two choices when allocating an array... 1. use a dynamic array like so: int[] a = new int[256]; 2. use a static array like so: int[256] a; both the above allocate an array of 256 int's. Given that, it does not seem possible to use a typedef as you have described, but, it also seems pointless to me. You can go: typedef int[256] vint; vint a; if you want vint to mean "a static array of length 256". ReganIn article <cbv4bl$14om$1 digitaldaemon.com>, Ben Hinkle says...-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/the same error happens if you take out the typedef and just do: int main() { int* vector = new int[]; return 0; } so you see the key is you need to specify how big an array to allocate - not just that you want to allocate an array.
Jun 30 2004
Static arrays cannot be created using new. Static arrays have types with a fixed dimension. new can only make dynamic arrays. To create a 256 byte dynamic array using new: ubyte[] vec; vec = new ubyte[256]; "join" <join_member pathlink.com> wrote in message news:cbvem7$1jon$1 digitaldaemon.com...I've got some bug in me... :) int main (char[][] args) { ubyte[256] vec1 = new ubyte[256]; alias ubyte[256] vec256; vec256* vec2 = new vec256; // [error]: new can only create structs, _arrays_ or class objects, not ubyte[256]'s // Is ubyte[256] not array? 8-) // Can we make new array at runtime? :D return 0; }
Jun 30 2004
On Wed, 30 Jun 2004 22:14:45 -0700, Walter <newshound digitalmars.com> wrote:Static arrays cannot be created using new. Static arrays have types with a fixed dimension. new can only make dynamic arrays. To create a 256 byte dynamic array using new: ubyte[] vec; vec = new ubyte[256];I think what he was trying to do was this ubyte[256]* vec2 = new ubyte[256]; with a typedef of typedef ubyte[256] vec256; eg vec256* vec2 = new vec256; alias does not work either, yet using text replacement both the above should be the same. Regan"join" <join_member pathlink.com> wrote in message news:cbvem7$1jon$1 digitaldaemon.com...-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/I've got some bug in me... :) int main (char[][] args) { ubyte[256] vec1 = new ubyte[256]; alias ubyte[256] vec256; vec256* vec2 = new vec256; // [error]: new can only create structs, _arrays_ or class objects, not ubyte[256]'s // Is ubyte[256] not array? 8-) // Can we make new array at runtime? :D return 0; }
Jun 30 2004
"Walter" <newshound digitalmars.com> wrote in message news:cc070c$2osf$1 digitaldaemon.com...Static arrays cannot be created using new. Static arrays have types with a fixed dimension. new can only make dynamic arrays. To create a 256 byteI hope this will change in the future (dmd 1.3?) because it would be very useful. You don't allways know the dimensions at compiletime. Is there even the smallest possibillity that we will be able to alocate rectangular arrays dynamically?dynamic array using new: ubyte[] vec; vec = new ubyte[256]; "join" <join_member pathlink.com> wrote in message news:cbvem7$1jon$1 digitaldaemon.com...I've got some bug in me... :) int main (char[][] args) { ubyte[256] vec1 = new ubyte[256]; alias ubyte[256] vec256; vec256* vec2 = new vec256; // [error]: new can only create structs, _arrays_ or class objects, not ubyte[256]'s // Is ubyte[256] not array? 8-) // Can we make new array at runtime? :D return 0; }
Jul 01 2004
Ivan Senji wrote:"Walter" <newshound digitalmars.com> wrote in message news:cc070c$2osf$1 digitaldaemon.com...It is the very definition of a static array that you know the dimensions of compile time.Static arrays cannot be created using new. Static arrays have types with a fixed dimension. new can only make dynamic arrays. To create a 256 byteI hope this will change in the future (dmd 1.3?) because it would be very useful. You don't allways know the dimensions at compiletime.Is there even the smallest possibillity that we will be able to alocate rectangular arrays dynamically?What you want is not allocating static arrays dynamically (which would be rather pointless) but having rectangular dynamic arrays. The proposal for that was deferred to 2.0 by Walter.
Jul 01 2004
"Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message news:cc0emc$46k$2 digitaldaemon.com...Ivan Senji wrote:with"Walter" <newshound digitalmars.com> wrote in message news:cc070c$2osf$1 digitaldaemon.com...Static arrays cannot be created using new. Static arrays have typesbytea fixed dimension. new can only make dynamic arrays. To create a 256ofI hope this will change in the future (dmd 1.3?) because it would be very useful. You don't allways know the dimensions at compiletime.It is the very definition of a static array that you know the dimensionscompile time.That is exactly what i meant but now i see it isn't what i wrote:) it happens!Is there even the smallest possibillity that we will be able to alocate rectangular arrays dynamically?What you want is not allocating static arrays dynamically (which would be rather pointless) but having rectangular dynamic arrays. The proposal for that was deferred to 2.0 by Walter.
Jul 01 2004
join wrote:// [error]: new can only create structs, _arrays_ or class objects, not ubyte[256]'sI believe that error message is ambiguous. It should specifically say _dynamic arrays_ which can indeed be new'd, although they don't require it (just set .length or use ~). For static arrays, you simply declare the variable, as with the basic types. So, for your vint type, just do... <snip> typedef int[256] vint; vint vector; </snip> That's it. The whole thing. No new'ing involved. The 'vector' variable is now an array of 256 ints, with typename 'vint'. -Chris S. -Invironz
Jun 30 2004
In article <opsafltjg25a2sq9 digitalmars.com>, Regan Heath says...new vec256 returns an array reference, not a pointer, you have to go alias ubyte[256] vec256; ubyte[] tmp = new vec256; vec256* vec2 = &tmp;//and... ubyte[256] tmp = new vec256; We've got the same error here: ubyte[256] is not an array. But this works: alias ubyte[256] vec256; ubyte[256] tmp = new ubyte[256]; vec256* vec2 = &tmp; It looks like "alias" doesn’t work at all here, as "typedef". So this scratch from docs sounds strange: "Aliased types are semantically identical to the types they are aliased to. The debugger cannot distinguish between them, and there is no difference as far as function overloading is concerned." In article <cc070c$2osf$1 digitaldaemon.com>, Walter says...Static arrays cannot be created using new. Static arrays have types with a fixed dimension. new can only make dynamic arrays.OK. I get you. But we are talking about aliasing and typedefing witch doesn’t work with "new" at all: alias ubyte[256] vec256; ubyte[] tmp1 = new ubyte[256]; //[OK] ubyte[] tmp2 = new vec256; //[ERROR] In article <cc0cqh$31k5$1 digitaldaemon.com>, Ivan Senji says...I hope this will change in the future (dmd 1.3?) because it would be very useful. You don't allways know the dimensions at compiletime. Is there even the smallest possibillity that we will be able to alocate rectangular arrays dynamically?Does this mean that we cannot allocate any array at runtime? Can we make some array trees at runtime? i.e. for example of my problem: typedef atom*[256] node; // node is [atom* x 256] typedef node* atom; // atom is node* (I've got an error here and I mast rewrite this as sructs not typedefs ... Yes? But it understandable for example.) It can looks like: node[...] null node[...] ^ ^ ^ | | | node[atom*, atom*, ... atom*] <-- the top of the tree This means: atom may point to next node or may not (null). And this makes some array trees in memory. Nodes can be newed or deleted at runtime. Can I somehow realize it in D?
Jul 01 2004