English 中文(简体)
如果我为 C++ 中的“ std::string &” 类型“ std::string &” 的参数指定默认值, 会不会导致内存泄漏?
原标题:If I specify a default value for an argument of type "std::string &" in C++, could that cause a memory leak?

我有一个方法被宣布为这样:

/*!
rief Removes the leading and trailing white space from a string.
param s The string to remove the white space from.
param white_chars Characters to be considered as whitespace.
*/
std::string Trim(const std::string &s, const std::string &white_chars = " 	

");

方法的定义并不令人感兴趣,但这里是:

std::string Trim(const std::string &s, const std::string &white_chars)
{
    size_t startidx = s.find_first_not_of(white_chars);
    if (startidx == std::string::npos) return "";
    size_t endidx = s.find_last_not_of(white_chars);
    return s.substr(startidx, endidx - startidx + 1);
}

现在,在大多数使用这种方法时,我只提出第一个论点。Valgrind给我以下警告:

==3338== 68 bytes in 2 blocks are possibly lost in loss record 4,639 of 7,212
==3338==    at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3338==    by 0x728CA88: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==3338==    by 0x728E2B4: char* std::string::_S_construct<char*>(char*, char*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==3338==    by 0x728E414: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&, unsigned long, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==3338==    by 0x728E441: std::string::substr(unsigned long, unsigned long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==3338==    by 0x6965F0A: str::Trim(std::string const&, std::string const&) (appbase.cpp:90)
==3338==    by 0x5F481D7: netopt::Element::set_ID(std::string const&) (netopt_elem.cpp:85)

注意"路障可能丢失了"?我见过 Valgrind告诉我的"路障肯定是丢失了"

< 坚固 > 因此问题就变成, 我是不是通过给 std:: string & amp; 指定默认值来造成内存泄漏? 如果是这样, 我做错什么了?

最佳回答

不存在技术问题, 但哲学上在每次调用时创建临时 < code>std:: 字符串 并不那么好 。 特别是 libstdc++ ( 你似乎正在使用 ), 因为它每次都会造成记忆分配( 没有短字符串优化 ) 。

由于 find_first_not_of 的超负荷使用 char const/code>, 最好提供两个超载代替:

// First, the one with the default argument, but without a temporary
std::string Trim(std::string const& s, char const* white_chars = " 	

");

// Second, for convenience, one with a `std::string`
std::string Trim(std::string const& s, std::string const& white_chars);

这也意味着,当调用 Trim(xx, "abc") 时,您将避免生成临时 std::string :)

当然,超技术解决办法是重新使用已经写好的代码:Boost String Algorithm 有许多用于字符串操纵的算法,包括code>trim 。

问题回答

没问题

临时字符串是在每次调用时构造的, 并在语句结尾处自动再次销毁。 只要你不保存( 正在调试的) 引用, 就不会有问题 。

In general, only the new/malloc without delete/free will cause the memory leak. In your case, no new called, so should no memory leak. The " " is in stack memory and reused each time to crate a instance for std::string &white_chars

在堆叠中创建的临时实例, 并在功能返回后释放。 也没有内存泄漏 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?