投射到指针 - 托管还是非托管? [英] Casting to pointer - managed or unmanaged?

查看:107
本文介绍了投射到指针 - 托管还是非托管?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





假设我有以下代码:

Hi,

Let's say I have the following code:

consoleHelper.write_status("Error: " + (char*)gai_strerror(err_code));



在那里有一个char *,并且没有调用 delete 。这个内存是以某种方式自动管理的,还是你必须做以下的事情?


There's a cast there to a char* and there's no delete called on it. Is this memory somehow automatically managed, or would you have to do something like the following?

char* cast_ch = (char*)gai_strerror(err_code);
consoleHelper.write_status("Error: " + cast_ch);

delete cast_ch;



即使使用的内存很小,我也是d希望尽可能防止所有内存泄漏,以确保应用程序理论上可以永久运行而不会出现问题。



我尝试过: < br $>


没什么,只是征求意见


Even if the memory used is tiny, I'd like to prevent all memory leaks wherever possible to ensure the application can theoretically run forever with no issues.

What I have tried:

Nothing, just asking for advice

推荐答案

你不应该释放内存(它不是明确要求的文档,而且,你不知道图书馆实际如何分配它。)





请注意:如果有泄漏,这是(不太可能)图书馆的作者错误(可能无害)。另一方面,在指针上调用delete会很危险,完全由你负责。 : - )
You should NOT release memory (it is not explicitely required in the documentation and, moreover, you don't know how the library actually allocates it).


Please note: if there is a leak, that's a (unlikely) library's author mistake (and probably harmless). On the other hand, calling delete on the pointer would be dangerous and entirely of your responsibility. :-)


文档(您应该首先检查): gai_strerror [ ^ ]告诉你返回是一个指向字符常量的指针,因此你不应该对它做任何事情。
The documentation (which you should always check first) : gai_strerror[^] tells you that the return is a pointer to a character constant, so you should not do anything with it.


这篇关于投射到指针 - 托管还是非托管?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆