Memory management by interfacing C/C++
Ferhat Kurtulmuş
aferust at gmail.com
Mon Apr 29 14:20:12 UTC 2019
On Monday, 29 April 2019 at 00:53:34 UTC, Paul Backus wrote:
> On Sunday, 28 April 2019 at 23:10:24 UTC, Ferhat Kurtulmuş
> wrote:
>> You are right. I am rewriting the things using mallocs, and
>> will use core.stdc.stdlib.free on d side. I am not sure if I
>> can use core.stdc.stdlib.free to destroy arrays allocated with
>> new op.
>
> core.stdc.stdlib.free is (as the name suggests) the standard C
> `free` function. As such, it can only be used to free memory
> allocated by the standard C functions `malloc`, `calloc`, and
> `realloc`. This is the same in D as it is in C and C++.
Thank you. It is now like:
/* c/cpp side */
extern (C) void deleteArr(void* arr);
void deleteArr(void* arr){
delete[] arr;
}
struct IntVector Subdiv2D_GetLeadingEdgeList(Subdiv2D sd){
std::vector<int> iv;
sd->getLeadingEdgeList(iv);
int *cintv = new int[iv.size()];
for(size_t i=0; i < iv.size(); i++){
cintv[i] = iv[i];
}
IntVector ret = {cintv, (int)iv.size()};
return ret;
};
/* c/cpp side */
...
int[] getLeadingEdgeList(){ // d function
IntVector intv = Subdiv2D_GetLeadingEdgeList(this);
int[] ret = intv.val[0..intv.length].dup;
deleteArr(intv.val);
return ret;
}
...
More information about the Digitalmars-d-learn
mailing list