473,803 Members | 3,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

str().c_str() question


Consider:

bool transmit ( const char* pch, size_t len )
{
int const val = strcmp( pch, "who_am_i" );
if ( val == 0 )
return ( true );
return ( false );
}

int main ( void )
{
std::stringstre am str;
str << "who_am_i";
size_t const sz = str.str().size( ) ; // the temporary created is
destroyed
char* ftest = new char [ sz + 1 ];
strcpy( ftest, str.str().c_str () );
std::cout << std::hex
<< static_cast<int >(ftest[0])
<< ""
<< static_cast<int >(ftest[1])
<< std::endl;

if ( transmit ( str.str().c_str (), str.str().size( )) )
{ std::cout << " success " << std::endl; }
else
{ std::cout << " failure " << std::endl; }

}

At issue:
str.str().c_str ().

If I did:
const char* ptr_me = str.str().c_str ();

The temporary returned from .str() is destroyed at the end of the full
expression, and it takes the buffer used by .c_str() with it. Not
good.

The question then becomes, where's the end of the full expression for
the case where I call the transmit function. Is that at the
terminating brace of the transmit function?
In my mind the temporary should be destroyed at the opening brace of
transmit but that doesn't appear to be the case.

Apr 6 '06 #1
2 6457
ma740988 wrote:
Consider:

bool transmit ( const char* pch, size_t len )
{
[..]
return ( false );
You really don't need parentheses here, you know that, right?
}

int main ( void )
'void' is also superfluous. All those things just make your code
a bit less readable.
{
std::stringstre am str;
[...]
if ( transmit ( str.str().c_str (), str.str().size( )) ) .. ^______________ _______________ _______________ ___^
.. This is your "full expression"
{ std::cout << " success " << std::endl; }
else
{ std::cout << " failure " << std::endl; }

}

At issue:
str.str().c_str ().

If I did:
const char* ptr_me = str.str().c_str ();

The temporary returned from .str() is destroyed at the end of the
full expression, and it takes the buffer used by .c_str() with it.
Not good.

The question then becomes, where's the end of the full expression for
the case where I call the transmit function. Is that at the
terminating brace of the transmit function?
In my mind the temporary should be destroyed at the opening brace of
transmit but that doesn't appear to be the case.


The function call is part of the full expression. The full expression
is bound on the right by the closing parenthesis of the 'if' statement.

V
--
Please remove capital As from my address when replying by mail
Apr 6 '06 #2

Victor Bazarov wrote:
ma740988 wrote: [..]

The function call is part of the full expression. The full expression
is bound on the right by the closing parenthesis of the 'if' statement.

Thanks Vic

Apr 7 '06 #3

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

Similar topics

9
2135
by: Ingo Nolden | last post by:
Hi there, I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl. For a kernel which is not using mfc I am writing a serialization. For some reasons I want to implement it on my own. One goal is to make it robust against version changes and corrupted files. That means, when an object cannot be deserialized, it should be skipped. The validity of the remaining document can be verified...
15
11614
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 so they can return a pointer to their internal buffer, or are they equally likely to create a new buffer on demand? I know the standard doesn't require any particular implementation, which is why I'm curious if there is a consensus among...
2
3095
by: Vyacheslav Kononenko | last post by:
All, If I am not mistaken I had some problems with code like this: std::string foo, bar; .... somefunc( foo.c_str(), bar.c_str() ); Problem was that c_str() used buffer shared btw instances of std::string in that implementation. I did not find anything that
2
3669
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
5
1933
by: Fred Paris | last post by:
Hi I'm writing a class to act as a wrapper around a C library. This C library exposes functions like: SetSomeInfo( char *pTheInfo ); In my wrapper class, the info in question is in a STL string.
18
6886
by: Martin Pöpping | last post by:
Hello, I´ve a problem with the follwing code lines: string line; ifstream myfile ("doorcoordinates.txt"); if (myfile.is_open()) { char str; char * pch;
7
839
by: DaVinci | last post by:
//error happen even when there existe a a.txt file #include<fstream> #include <string> #include <iostream> using std::string; using std::fstream; int main() { string s = "a.txt"; fstream file(s.c_str(),std::ios_base::in | std::ios_base::app);
12
5285
by: shyam | last post by:
Hi I have a program within which i have the following statement. sprintf(somevar,"%s/%s.%s",PREFIX.c_str( ),MIDDLE.c_str( ),SUFFIX.c_str( ) ); Here PREFIX, MIDDLE and SUFFIX are all const strings. This works fine but recently after close to 1000 successful runs my
6
2999
by: jabbah | last post by:
I want to use a stringstream internally, but to conform to an api I want to return a char*. Only this does not work: #include <iostream> #include <string> #include <sstream> using namespace std;
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.