Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
September 11, 2002 Interfacing to C: Declaring a pointer to an array of pointers | ||||
---|---|---|---|---|
| ||||
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? |
September 12, 2002 Re: Interfacing to C: Declaring a pointer to an array of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | 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.
|
September 12, 2002 Re: Interfacing to C: Declaring a pointer to an array of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Burton Radons | 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...
|
September 12, 2002 Re: Interfacing to C: Declaring a pointer to an array of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | "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; |
September 13, 2002 Re: Interfacing to C: Declaring a pointer to an array of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | 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];
|
Copyright © 1999-2021 by the D Language Foundation