Will this leak memory?

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 ?
}

Tks
 
J

Jonathan Mcdougall

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
 
T

Torsten Mueller

Jonathan Mcdougall said:
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.
 
J

Jonathan Mcdougall

char* s = str.c_str() ; //<- presumably a call to calloc/malloc
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
 
A

Alfonzo Morra

Alfonzo said:
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 !
 
A

Alf P. Steinbach

* 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... ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top