www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - typedef & new

reply join <join_member pathlink.com> writes:
Hello.
One question it is.
Why I cannot do something like this:

typedef int[] vint;
vint vector = new vint;   // [error]

?
Jun 30 2004
next sibling parent Hauke Duden <H.NS.Duden gmx.net> writes:
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
prev sibling parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
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
parent reply join <join_member pathlink.com> writes:
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
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
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
---------
Jun 30 2004
parent reply join <join_member pathlink.com> writes:
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
next sibling parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"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.
Jun 30 2004
prev sibling parent reply Regan Heath <regan netwin.co.nz> writes:
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/
Jun 30 2004
parent reply join <join_member pathlink.com> writes:
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/
Jun 30 2004
next sibling parent Regan Heath <regan netwin.co.nz> writes:
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/
Jun 30 2004
prev sibling next sibling parent reply "Walter" <newshound digitalmars.com> writes:
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
next sibling parent Regan Heath <regan netwin.co.nz> writes:
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...
 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;
 }
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 30 2004
prev sibling parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"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 byte
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?
 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
parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
Ivan Senji wrote:

 "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 byte
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.
It is the very definition of a static array that you know the dimensions of compile time.
 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
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message
news:cc0emc$46k$2 digitaldaemon.com...
 Ivan Senji wrote:

 "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
byte
 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.
It is the very definition of a static array that you know the dimensions
of
 compile time.

 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.
That is exactly what i meant but now i see it isn't what i wrote:) it happens!
Jul 01 2004
prev sibling parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
join wrote:
 // [error]: new can only create structs, _arrays_ or class objects, not
 ubyte[256]'s
I 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
parent join <join_member pathlink.com> writes:
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