Return dynamically allocated memory from C++ to C

前端 未结 10 2302
粉色の甜心
粉色の甜心 2021-01-01 07:10

I have a dll that must be useable from C etc, so I cant use string objects etc as a normal would, but I\'m not sure on how to do this safely..

const char *Ge         


        
10条回答
  •  耶瑟儿~
    2021-01-01 07:51

    Well obviously anytime you are returning pointers to memory allocated inside a function the deallocating must come externally, unless you are using garbage collection. If you don't want to do this, allocate a character buffer b efore calling GetString() and change the prototype to

    int get_string(const char* buffer);

    Then fill up the buffer. But returning a point to malloced data is fine.

提交回复
热议问题