By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,479 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,479 IT Pros & Developers. It's quick & easy.

Will this leak memory?

P: n/a
MyClass::foo ( const std::string str) {
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...

...
//Do I need a free(s) here to free memory allocated ?
}

Tks

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> MyClass::foo ( const std::string str) {

Passing by constant value is unnecessary and misleading at best.
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...
Perhaps, but that is not your business. This should be

const char *s = std.c_str();

What std::string::c_str() returns is owned by the string and you may
not modify it in any way.
//Do I need a free(s) here to free memory allocated ?


No. That's illegal/unnecessary/bad for your health.
Jonathan

Jul 23 '05 #2

P: n/a
"Jonathan Mcdougall" <jo***************@gmail.com> schrieb:
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...


const char *s = std.c_str();


Because the pointer is defined as const it will *never* cause any
leak. You can call c_str() as often as you want.

T.M.
Jul 23 '05 #3

P: n/a
> > > char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...
const char *s = std.c_str();


Because the pointer is defined as const it will *never* cause any
leak.


It will never cause any leak, but that's not because it is const (you
can delete const pointers), it's because the standard says it.
You can call c_str() as often as you want.


Yes.
Jonathan

Jul 23 '05 #4

P: n/a


Alfonzo Morra wrote:
MyClass::foo ( const std::string str) {
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...

...
//Do I need a free(s) here to free memory allocated ?
}

Tks


Thanks guys !

Jul 23 '05 #5

P: n/a
* Alfonzo Morra:
MyClass::foo ( const std::string str) {
char* s = str.c_str() ; //<- presumably a call to calloc/malloc
// behind the scenes ...

...
//Do I need a free(s) here to free memory allocated ?
}


First, to clear up some remarks made by another poster:

* Adding 'const' to pass-by-value can be helpful to detect the
common '=' instead of '==' typo. But be aware that the 'const'
does not, in this case, affect the function signature. Also,
it may be more efficient to pass by reference, i.e.

std::string const& str

and that is how it's usually done.

* The declaration of 's' should be

char const* const s = str.c_str();

The first 'const' refers to the contents of the string; you can
get away without it only because of an old C compatibility feature
stemming from the time before 'const' was introduced. The second
'const' refers to the pointer itself, ensuring that this pointer
is not reassigned to point to something else.

* The 'const's have nothing to do with memory allocation/deallocation.

The pointer you obtain is valid until you change the contents of 'str'; it
is the 'str' object that is responsible for deallocation, if any is needed.

Since 'str' is 'const' and by value a change of 'str' won't happen, so the
pointer is guaranteed to be valid till the end of the function body.

When you pass by reference you run the risk of aliasing, that in some way
the code may change the contents of 'str' via some non-'const' reference,
e.g. a global reference, and in that case the pointer can be become invalid.
However, ordinarily that's not a problem. Strings are simply not used in
ways that can ordinarily cause aliasing, and anyway, globals are Evil... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.