473,396 Members | 1,760 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.

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::stringstream 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 6435
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::stringstream 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
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...
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...
2
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...
2
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
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...
18
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
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...
12
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...
6
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.