Does c_str property of string class allocate memory?

A

Alfonso Morra

Hi,

Doest the c_str() method of the string class allocate memory. if it does
alloc mem behind the scenes - whose responsibility is it to cleanup
after (i.e. free the returned char*?)

Tkx
 
P

peter.koch.larsen

Alfonso said:
Hi,

Doest the c_str() method of the string class allocate memory. if it does
alloc mem behind the scenes - whose responsibility is it to cleanup
after (i.e. free the returned char*?)

Tkx
This is not specified in the standard, so the answer is "could be", but
I haven't yet met a string which did allocate.
In all cases you are not responsible for the deallocation.

/Peter
 
R

Robbie Hatley

Alfonso Morra said:
Hi,

Doest the c_str() method of the string class allocate memory. if it does
alloc mem behind the scenes - whose responsibility is it to cleanup
after (i.e. free the returned char*?)

Tkx

That's up to the compiler author.

You don't need to worry about allocation or deallocation;
that's all handled for you automatically.

Some warnings about c_str() :

If you do something like this:

std::string Catz = "Round and round she goes!";
const char* Batz = Catz.c_str();

Then Batz is a pointer to an object which is owned by the
std::string object Catz. This means you need to remember
these two things:

1. You can NOT write to the location pointed to by Batz!
It's read-only. In fact, a good compiler won't even
let you omit the "const" in the definition of Batz above.

2. If Catz goes out of scope, Batz is no-longer guaranteed
to point to anything useful. Dereferencing Batz at that
point might crash your program (or your system).


Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
A

Alfonso Morra

Robbie said:
That's up to the compiler author.

You don't need to worry about allocation or deallocation;
that's all handled for you automatically.

Some warnings about c_str() :

If you do something like this:

std::string Catz = "Round and round she goes!";
const char* Batz = Catz.c_str();

Then Batz is a pointer to an object which is owned by the
std::string object Catz. This means you need to remember
these two things:

1. You can NOT write to the location pointed to by Batz!
It's read-only. In fact, a good compiler won't even
let you omit the "const" in the definition of Batz above.

2. If Catz goes out of scope, Batz is no-longer guaranteed
to point to anything useful. Dereferencing Batz at that
point might crash your program (or your system).


Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
tkx
 
D

Default User

Robbie Hatley wrote:

Some warnings about c_str() :

If you do something like this:

std::string Catz = "Round and round she goes!";
const char* Batz = Catz.c_str();

Then Batz is a pointer to an object which is owned by the
std::string object Catz. This means you need to remember
these two things:

1. You can NOT write to the location pointed to by Batz!
It's read-only. In fact, a good compiler won't even
let you omit the "const" in the definition of Batz above.

2. If Catz goes out of scope, Batz is no-longer guaranteed
to point to anything useful. Dereferencing Batz at that
point might crash your program (or your system).

Worse than that, if there's any call to a non-const member function of
Catz, then Batz is no longer valid. It's a poor idea to EVER try to
keep the pointer returned. You should always get a fresh copy before
using it.




Brian
 
M

Mike Wahler

Alfonso Morra said:
Hi,

Doest the c_str() method of the string class allocate memory.

Maybe, maybe not.
if it does alloc mem behind the scenes - whose responsibility is it to
cleanup after (i.e. free the returned char*?)

The std::string class handles all its own memory management,
as do all the other standard library types.

-Mike
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top