Re: another memory leak, weird`
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Tue, 18 Sep 2007 22:11:21 -0400
"Vivienne" <zhoudan.bupt@xxxxxxxxx> wrote in message
news:1190167122.240652.254120@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
I test the below function. memory leak occured ...
void testleak{
std::string a;
std::cin >> a;
_CrtDumpMemoryLeaks();
}
when I input a few charactors, like 10 charactors, no memory leak
occured here. but when I input more charactors, like 20, memory leak
occured!
why?
The string 'a' has not yet gone out of scope and has not been destroyed
by the time you call _CrtDumpMemoryLeaks. Any memory it has allocated on
the heap is (incorrectly) reported as a leak.
It so happens that a particular implementation of std::string you are
using employs so called small string optimization: strings shorter than
a certain limit are stored directly inside the string object itself,
thus not requiring a heap allocation. Memory for longer strings is still
allocated on the heap. That's why you see a false leak report for long
strings but not for short ones.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- References:
- another memory leak, weird`
- From: Vivienne
- another memory leak, weird`
- Prev by Date: another memory leak, weird`
- Next by Date: Re: another memory leak, weird`
- Previous by thread: another memory leak, weird`
- Next by thread: Re: another memory leak, weird`
- Index(es):
Relevant Pages
|