www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Can't create new dynamic array

reply Derek Parnell <derek psych.ward> writes:
This <code>
void main()
{
    char[]* a = new char[];
}
</code>

gives this error message:

test.d(3): new can only create structs, dynamic arrays or class objects,
not char[]'s

But isn't 'char[]' a dynamic array?

-- 
Derek Parnell
Melbourne, Australia
http://www.dsource.org/projects/build/ v2.06 released 04/May/2005
http://www.prowiki.org/wiki4d/wiki.cgi?FrontPage
5/05/2005 10:15:38 AM
May 04 2005
next sibling parent reply "Uwe Salomon" <post uwesalomon.de> writes:
 This <code>
 void main()
 {
     char[]* a = new char[];
 }
 </code>

 gives this error message:

 test.d(3): new can only create structs, dynamic arrays or class objects,
 not char[]'s

 But isn't 'char[]' a dynamic array?

No, char[] is a dynamic array reference. char[count] is a dynamic array. Ciao uwe
May 05 2005
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 05 May 2005 09:26:35 +0200, Uwe Salomon wrote:

 This <code>
 void main()
 {
     char[]* a = new char[];
 }
 </code>

 gives this error message:

 test.d(3): new can only create structs, dynamic arrays or class objects,
 not char[]'s

 But isn't 'char[]' a dynamic array?

No, char[] is a dynamic array reference. char[count] is a dynamic array.

Not according to the D documentation ... http://www.digitalmars.com/d/arrays.html -------------------------------------------- There are four kinds of arrays: int* p; Pointers to data int[3] s; Static arrays int[] a; Dynamic arrays int[char[]] x; Associative arrays Pointers ----------- int* p; These are simple pointers to data, analogous to C pointers. Pointers are provided for interfacing with C and for specialized systems work. There is no length associated with it, and so there is no way for the compiler or runtime to do bounds checking, etc., on it. Most conventional uses for pointers can be replaced with dynamic arrays, out and inout parameters, and reference types. Static Arrays ---------------- int[3] s; These are analogous to C arrays. Static arrays are distinguished by having a length fixed at compile time. Dynamic Arrays ----------------- int[] a; Dynamic arrays consist of a length and a pointer to the array data. Multiple dynamic arrays can share all or parts of the array data. ---------------------------- -- Derek Parnell Melbourne, Australia 5/05/2005 7:36:49 PM
May 05 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
Derek Parnell wrote:
 On Thu, 05 May 2005 09:26:35 +0200, Uwe Salomon wrote:
    char[]* a = new char[];

gives this error message:

test.d(3): new can only create structs, dynamic arrays or class objects,
not char[]'s

But isn't 'char[]' a dynamic array?

No, char[] is a dynamic array reference. char[count] is a dynamic array.

Not according to the D documentation ...

When used with the 'new' directive you must supply an initial size. To create a default-style dynamic array just supply size zero. Also note that the 'new' directive does not return a pointer, but a normal array referance. So replace char[]* a = new char[]; with char[] a = new char[0]; Yeah the need for the 0 seems silly to me too. -- Chris Sauls
May 05 2005
parent "Uwe Salomon" <post uwesalomon.de> writes:
 But isn't 'char[]' a dynamic array?

No, char[] is a dynamic array reference. char[count] is a dynamic array.


When used with the 'new' directive you must supply an initial size. To create a default-style dynamic array just supply size zero. Also note that the 'new' directive does not return a pointer, but a normal array referance. So replace char[]* a = new char[]; with char[] a = new char[0]; Yeah the need for the 0 seems silly to me too.

What are you doing here? a is dynamic array reference, that means it consists of a pointer and a size. The initial values for both are null. If you call new type[size], the memory manager allocates the size elements of the type in memory and sets the pointer in a to the first element and the size in a to size. Why would you want to set a to new char[0]??? Point to an empty array? You do not have to initialize a this way, just declare it without initialization, or set it to null: char[] a = null; And what i said still applies, you are mixing 2 different things: char[26] a; // static array char[] a; // dynamic array Thats what the reference says, but: a = new char[26]; This creates a new array of 26 char in memory, and adjusts the pointer and size in a (i'm sure you know that) -- so this creates a _dynamic_ array. You could also write: a = new char[sizeParam]; See? Dynamic. But if you write this: x = new char[]; The compiler thinks that you want to allocate a dynamic array _reference_. That means that x would have to be of type: (char[])* x; That's a pointer to a dynamic array reference. But the compiler won't let you allocate a dynamic array reference, and that's what the error message tries to tell you. If you really need one (i never could imagine a use for that), pack it into a struct: struct ArrayReference { char[] value; } ArrayReference* x = new ArrayReference; x.value = new char[300]; See? Ciao uwe
May 08 2005
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Uwe Salomon wrote:
 This <code>
 void main()
 {
     char[]* a = new char[];
 }
 </code>


 No, char[] is a dynamic array reference. char[count] is a dynamic array.

Actually, the OP's code is a version of the syntactic sugar int* a = new int; for int* a = new int[1]; Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
May 05 2005
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"Derek Parnell" <derek psych.ward> wrote in message
news:1k7qod8mk4p53.1en3c3ighdymp$.dlg 40tude.net...
 This <code>
 void main()
 {
     char[]* a = new char[];
 }
 </code>

 gives this error message:

 test.d(3): new can only create structs, dynamic arrays or class objects,
 not char[]'s

 But isn't 'char[]' a dynamic array?

You're trying to assign to a pointer to a dynamic array. Therefore, operator new needs to know how many of these dynamic arrays to allocate. This works: char[]* a = new char[][1]; // allocate space for 1 char[].
May 20 2005