www.digitalmars.com         C & C++   DMDScript  

D - Interfacing to C: Declaring a pointer to an array of pointers

reply Russell Lewis <spamhole-2001-07-16 deming-os.org> writes:
My D-space code makes use of an array of pointers.  I want to allocate 
this array on the heap (not the stack), cast the address to a void 
pointer, and send it through some C code.  Later, the C code will give 
the pointer back to me, I will cast it back and get access to my array.

I can declare the pointer-to-array-to-pointers variable, but am having 
trouble using "new" to allocate the memory:
     MyType*[]* ptr = new MyType*[];

Looks like the parser doesn't recognize []'s in the 'new' expression. 
How should I do this?
Sep 11 2002
next sibling parent reply Burton Radons <loth users.sourceforge.net> writes:
Russell Lewis wrote:

 My D-space code makes use of an array of pointers.  I want to allocate 
 this array on the heap (not the stack), cast the address to a void 
 pointer, and send it through some C code.  Later, the C code will give 
 the pointer back to me, I will cast it back and get access to my array.
 
 I can declare the pointer-to-array-to-pointers variable, but am having 
 trouble using "new" to allocate the memory:
     MyType*[]* ptr = new MyType*[];
 
 Looks like the parser doesn't recognize []'s in the 'new' expression. 
 How should I do this?

MyType *[] *ptr = (MyType *[] *) new void *[1]; That should do it, unless if my brain has melted and I've written gibberish. It's definitely a problem inherited from DMD; new needs some work.
Sep 11 2002
parent reply Russell Lewis <spamhole-2001-07-16 deming-os.org> writes:
Burton Radons wrote:
 Russell Lewis wrote:
 
 My D-space code makes use of an array of pointers.  I want to allocate 
 this array on the heap (not the stack), cast the address to a void 
 pointer, and send it through some C code.  Later, the C code will give 
 the pointer back to me, I will cast it back and get access to my array.

 I can declare the pointer-to-array-to-pointers variable, but am having 
 trouble using "new" to allocate the memory:
     MyType*[]* ptr = new MyType*[];

 Looks like the parser doesn't recognize []'s in the 'new' expression. 
 How should I do this?

MyType *[] *ptr = (MyType *[] *) new void *[1]; That should do it, unless if my brain has melted and I've written gibberish. It's definitely a problem inherited from DMD; new needs some work.

Actually, the size of an array reference is 8 bytes, right? So it should be: = (MyType*[]*) new void*[2]; Then I have to initialize the array...
Sep 12 2002
parent Burton Radons <loth users.sourceforge.net> writes:
Russell Lewis wrote:

 Burton Radons wrote:
 
 Russell Lewis wrote:

 My D-space code makes use of an array of pointers.  I want to 
 allocate this array on the heap (not the stack), cast the address to 
 a void pointer, and send it through some C code.  Later, the C code 
 will give the pointer back to me, I will cast it back and get access 
 to my array.

 I can declare the pointer-to-array-to-pointers variable, but am 
 having trouble using "new" to allocate the memory:
     MyType*[]* ptr = new MyType*[];

 Looks like the parser doesn't recognize []'s in the 'new' expression. 
 How should I do this?

MyType *[] *ptr = (MyType *[] *) new void *[1]; That should do it, unless if my brain has melted and I've written gibberish. It's definitely a problem inherited from DMD; new needs some work.

Actually, the size of an array reference is 8 bytes, right? So it should be: = (MyType*[]*) new void*[2]; Then I have to initialize the array...

Ah right (I knew my brain had melted), then: = new MyType *[] [1]; Should be the right way. That fails, and it's a parsing bug. The two void pointers will be initialised to null, so it's all set up as an array. But it's not portably guaranteed to be an array. The full, correct way to do it that works is: alias MyType *[] MyTypePtrArray; = new MyTypePtrArray [1];
Sep 12 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
"Russell Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3D7FA2FB.2030106 deming-os.org...
 My D-space code makes use of an array of pointers.  I want to allocate
 this array on the heap (not the stack), cast the address to a void
 pointer, and send it through some C code.  Later, the C code will give
 the pointer back to me, I will cast it back and get access to my array.

 I can declare the pointer-to-array-to-pointers variable, but am having
 trouble using "new" to allocate the memory:
      MyType*[]* ptr = new MyType*[];

 Looks like the parser doesn't recognize []'s in the 'new' expression.
 How should I do this?

What new is doing is attempting to allocate a dynamic array, and the trailing [] doesn't have a dimension in it. You could try: alias MyType*[] foo; foo *ptr = new foo;
Sep 12 2002