473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Does c_str property of string class allocate memory?

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

Sep 27 '05 #1
5 8116

Alfonso Morra wrote:
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

Sep 27 '05 #2
"Alfonso Morra" <sw***********@the-ring.com> wrote in message news:dh**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
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
Sep 27 '05 #3


Robbie Hatley wrote:
"Alfonso Morra" <sw***********@the-ring.com> wrote in message news:dh**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
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

tkx

Sep 27 '05 #4
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
Sep 27 '05 #5

"Alfonso Morra" <sw***********@the-ring.com> wrote in message
news:dh**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
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
Sep 28 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas | last post by:
Does anyone know if the stl string class implements some sort of pooling or chunking of memory for performance optimization? I know that the Lucent SCL has this built in, but not sure if the same...
15
by: Derek | last post by:
I'm curious about the performance of string::c_str, so I'm wondering how it's commonly implemented. Do most std::string implementations just keep an extra char allocated for the NULL termination...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
6
by: Marcus | last post by:
I have a couple of classes looking like this: ======================================== public class AdminSettingsData { public string adminSettingsPath; public string adminSettingsFile;...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
30
by: nano2k | last post by:
Hi I need an efficient method to convert a string object to it's byte equivalent. I know there are LOTS of methods, but they lack in efficiency. All methods allocate new memory to create the byte...
8
by: Jess | last post by:
Hello, I'm doing an exercise that defines a new abstract class "Str" that has the same functions as "string" class, which holds a vector of chars. But since I'm also trying to define a "c_str()"...
3
by: C++Liliput | last post by:
I have a class of the type class A { private: std::string data; ....... public: const char* toString(); };
12
by: sas | last post by:
hi, i need that because the path functions for windows, like PathAppend and PathRemoveFileExt accept a writable zero terminated char*, but i didn't find that for std::string, with CString, i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.