July 01, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to join | 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; > } |
July 01, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to join | 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
|
July 01, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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/ |
July 01, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "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; > > } > > |
July 01, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | 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. |
July 01, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to C. Sauls | 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? |
July 01, 2004 Re: typedef & new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | "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! |
Copyright © 1999-2021 by the D Language Foundation