Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 30, 2004 typedef & new | ||||
---|---|---|---|---|
| ||||
Hello. One question it is. Why I cannot do something like this: typedef int[] vint; vint vector = new vint; // [error] ? |
June 30, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to join | 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
|
June 30, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to join | 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
---------
|
June 30, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | 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 >--------- > |
June 30, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to join | 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, 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 > >--------- > > > > |
June 30, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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. |
June 30, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to join | "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... > > > >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. > > |
June 30, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to join | 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". Regan > 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. > > -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
June 30, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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". > >Regan > >> 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. >> >> > > > >-- >Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
June 30, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to join | 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... >> >> 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". >> >> Regan >> >>> 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. >>> >>> >> >> >> >> -- >> Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ > > -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
Copyright © 1999-2021 by the D Language Foundation