Communication between D and C with dynamic arrays

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 10 09:28:12 PDT 2014


On Sunday, 10 August 2014 at 15:37:41 UTC, seany wrote:
> On Sunday, 10 August 2014 at 15:34:30 UTC, ketmar via 
> Digitalmars-d-learn wrote:
>
>> from D side -- yes. just don't store passed pointer on C side, 
>> 'cause
>> it can be changed on array resize.
>
> Excellent,
>
> So if I have
>
> int [] array;
> void * ptr_to_array = &array;

Don't do that. `&array` is not the same as `array.ptr`. Use
`array.ptr`. `&array` would be a pointer to the local variable
`array`.

> /* populate array here */
>
> C_Function(ptr_to_array);
>
> /* repopulate array here, the pointer itself may change, but 
> the variable ptr_to_array will be updated accordingly */
>
>
> C_Function(ptr_to_array);
>
> I am okey?

I'm not sure if you got that right: ptr_to_array will not be
updated automatically. You have to do that yourself.

Also, if C_Function has only one parameter which is a pointer,
make sure the array has the proper length, or that is has a
proper terminator element (char* arguments must usually be
zero-terminated).


More information about the Digitalmars-d-learn mailing list