我有一個DLL,它必須是由C等可用的,所以我不能使用字符串對象等作爲一個正常的可以,但我不知道如何做到這一點安全..返回動態分配的內存,從C++到C
const char *GetString()
{
std::stringstream ss;
ss << "The random number is: " << rand();
return ss.str().c_str();
}
當ss掉下堆棧時,c字符串是否會被銷燬?我假設如此...
另一種選擇可能是在堆上創建一個新的字符串,但到底是怎麼回事解除分配呢?
const char *GetString()
{
std::stringstream ss;
ss << "The random number is: " << rand();
char *out = new char[ss.str().size()];
strcpy(ss.str().c_str(), out);
return out;//is out ever deleted?
}
指向其他事物以及字符串的指針也是一樣。
好的,以及我的dll是python的actauly,那麼最好的方法是做什麼呢?我是否應該將dll函數封裝在說「Call dll function; Call dll deallocate function」的python函數中?我假設python使完整的新字符串,而不是隻是包裝周圍的對象?請問 – 2008-11-12 08:30:26