简体   繁体   English

返回一个char指针(内存泄漏)

[英]Return a char pointer (memory leak)

I have a function in my hardware code that returns a char* as such: 我的硬件代码中有一个函数,它返回一个char *:

char* getText();

I would like to know how this is actually working in the system. 我想知道这是如何在系统中实际工作的。 In my getText function , I allocated a memory space via alloc to a char* . 在我的getText function ,我通过alloc to a char*分配了一个内存空间。 Then, I simply returned it through that function. 然后,我只是通过该函数返回它。

NOW, I had another function retreive this by calling char* receive=getText() , and I deleted receive after making use of it. 现在,我通过调用char* receive=getText()检索另一个函数,并在使用它后删除了receive。 Can I check if this would causes any memory leak? 我可以检查这是否会导致任何内存泄漏?

Since I assume you are on a linux system using GCC to compile you could use valgrind to run your program and guarantee to find any leaks present or even possible memory leaks that could happen. 因为我假设您使用GCC编译的Linux系统,您可以使用valgrind来运行您的程序并保证找到任何泄漏,甚至可能发生的内存泄漏。

To answer your question more directly in this specific case, if you can guarantee that you free() receive after you are done using it then you will not have a memory leak. 要在这种特定情况下更直接地回答您的问题,如果您可以保证在完成使用后免费()接收,那么您将不会有内存泄漏。 However, if you forget to free() receive and then reassign something to receive, that right there is considered a memory leak. 但是,如果您忘记free()接收然后重新分配要接收的内容,那么就会考虑内存泄漏。 You have lost the handle to the resource you were responsible for freeing and it can no longer be freed. 您已经丢失了负责释放的资源的句柄,并且无法再释放它。

In general, your code is very C-like and not the C++ way of doing things. 通常,您的代码非常类似于C而不是C ++的处理方式。 Returning a std::string would be more C++-like. 返回一个std :: string会更像C ++。 In regards to dynamic allocation, malloc() and free() are also the "C way" of doing dynamic allocation. 关于动态分配,malloc()和free()也是进行动态分配的“C方式”。 new and delete are the C++ way of doing dynamic allocation. new和delete是进行动态分配的C ++方式。

As people suggest you, use std::string instead of C-like strings. 正如人们建议的那样,使用std::string而不是类似C的字符串。 std::string manages automatically its own memory, and has a rich and secure interface. std::string自动管理自己的内存,并具有丰富而安全的界面。 String is moveable by default, so by-value-return does not have any performance penalty. 字符串默认是可移动的,因此按值返回不会有任何性能损失。

But if you want to return a pointer (In other words, you want to return a "handle" to the string, not the string itself), consider to use smart-pointers instead of raw-pointers. 但是如果你想返回一个指针(换句话说,你想要为字符串返回一个“句柄”,而不是字符串本身),考虑使用智能指针而不是原始指针。 Smart pointers are leak-free, because its memory magnament is based on RAII . 智能指针是无泄漏的,因为它的内存标记基于RAII The problem with returning a raw-pointer is that the interface of your function not specifies who would deallocate the string. 返回原始指针的问题是函数的接口没有指定谁将解除分配字符串。 The caller, or a manager/factory that is used by the function? 调用者,或函数使用的经理/工厂?

If you use unique_ptr as return type, you are specifying that the result string will be deleted when the caller stop using its handle. 如果使用unique_ptr作为返回类型,则指定在调用者停止使用其句柄时将删除结果字符串。 On the other hand, if you use shared_ptr, you specifies that the string is managed by an internal manager/factory used by the function. 另一方面,如果使用shared_ptr,则指定该字符串由函数使用的内部管理器/工厂管理。

The point with smart pointers is that you do not have to worry about string lifetime/memory magnament . 智能指针的要点是你不必担心字符串生命周期/内存magnament

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM