473,385 Members | 1,593 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,385 software developers and data experts.

c_str() does a SEGSEGV

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
program dumped with SIGSEGV.

I did a bt on the core file at gdb prompt and found that the problem is
with c_str( )

I am using gcc 3.2.3
libstdc++.so.5

on RH linux AS 3.

Is the spriontf statement correct , or is there any better way.

thanks
Shyam

Sep 27 '06 #1
12 5238
Dnia 27 Sep 2006 02:47:52 -0700, shyam napisał(a):
sprintf(somevar,"%s/%s.%s",PREFIX.c_str( ),MIDDLE.c_str(
),SUFFIX.c_str( ) );
What is "somevar" ?
Is the spriontf statement correct , or is there any better way.
Did you try to use stringstream or normal string concatenation?

--
SirMike - http://www.sirmike.org

C makes it easy to shoot yourself in the foot; C++ makes it harder, but
when you do, it blows away your whole leg. - Bjarne Stroustrup
Sep 27 '06 #2
shyam wrote:
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
program dumped with SIGSEGV.

I did a bt on the core file at gdb prompt and found that the problem is
with c_str( )

I am using gcc 3.2.3
libstdc++.so.5

on RH linux AS 3.

Is the spriontf statement correct , or is there any better way.
I'd blame some of your (not shown) code that probably
overwrites some internal memory of the strings, hence
the c_str() segfault.

<OT>
If you are on a 32-bit platform, try valgrind to pinpoint
the error.
</OT>

HTH,
- J.
Sep 27 '06 #3
I havent used string or stringstream concatenation.
And i am not using these const strings in any other way.

I feel it is better to concatenate these three strings into one string
object and then so a c_str( ) on that object.

Jacek Dziedzic wrote:
shyam wrote:
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
program dumped with SIGSEGV.

I did a bt on the core file at gdb prompt and found that the problem is
with c_str( )

I am using gcc 3.2.3
libstdc++.so.5

on RH linux AS 3.

Is the spriontf statement correct , or is there any better way.

I'd blame some of your (not shown) code that probably
overwrites some internal memory of the strings, hence
the c_str() segfault.

<OT>
If you are on a 32-bit platform, try valgrind to pinpoint
the error.
</OT>

HTH,
- J.
Sep 27 '06 #4
shyam wrote:
I havent used string or stringstream concatenation.
And i am not using these const strings in any other way.

I feel it is better to concatenate these three strings into one string
object and then so a c_str( ) on that object.
Why? char* is not a #$*&@!@ string. It's a pointer to
a single character. This isn't C.

string somevar = PREFIX + '/' + MIDDLE + '.' + SUFFIX;

or

ostringstream os;
os << PREFIX << "/" << MIDDLE << "." << SUFFIX;
string somevar = os.str();

Sep 27 '06 #5

Ron Natalie wrote:
shyam wrote:
I havent used string or stringstream concatenation.
And i am not using these const strings in any other way.

I feel it is better to concatenate these three strings into one string
object and then so a c_str( ) on that object.

Why? char* is not a #$*&@!@ string. It's a pointer to
a single character. This isn't C.
You are correct but i want the final string in char * form and not C++
string
string somevar = PREFIX + '/' + MIDDLE + '.' + SUFFIX;

or

ostringstream os;
os << PREFIX << "/" << MIDDLE << "." << SUFFIX;
string somevar = os.str();
Sep 27 '06 #6

shyam wrote:
Ron Natalie wrote:
shyam wrote:
I havent used string or stringstream concatenation.
And i am not using these const strings in any other way.
>
I feel it is better to concatenate these three strings into one string
object and then so a c_str( ) on that object.
Why? char* is not a #$*&@!@ string. It's a pointer to
a single character. This isn't C.

You are correct but i want the final string in char * form and not C++
string
Well, it's your hair. You're problem could very well be a buffer
overrun. This is what happens when you use buffers...they eventually
bite you really hard.

Sep 27 '06 #7
On 27 Sep 2006 08:07:48 -0700, I waved a wand and this message
magically appears in front of shyam:
Why? char* is not a #$*&@!@ string. It's a pointer to
a single character. This isn't C.

You are correct but i want the final string in char * form and not C++
string
Use c_str() on the resulting string. Simple.
--
http://www.munted.org.uk

You've been eating the cat food again, haven't you?
Sep 27 '06 #8
Hi,
I'm not sure, but I think c_str() allocates new memory which has to be freed
by delete.
shyam wrote:
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
program dumped with SIGSEGV.

I did a bt on the core file at gdb prompt and found that the problem is
with c_str( )

I am using gcc 3.2.3
libstdc++.so.5

on RH linux AS 3.

Is the spriontf statement correct , or is there any better way.

thanks
Shyam
Sep 27 '06 #9
Thorsten Kiefer wrote:
Hi,
I'm not sure, but I think c_str() allocates new memory which has to be freed
by delete.
Not a 'delete' in user code, that's for sure.

- J.
Sep 27 '06 #10
Thorsten Kiefer wrote:
Hi,
I'm not sure, but I think c_str() allocates new memory which has to
be freed by delete.
If c_str() allocates extra memory, the std::string has to handle the
deallocation itself. User code has to do nothing.
Bo Persson
Sep 27 '06 #11
shyam wrote:
Ron Natalie wrote:
>shyam wrote:
>>I havent used string or stringstream concatenation.
And i am not using these const strings in any other way.

I feel it is better to concatenate these three strings into one string
object and then so a c_str( ) on that object.
Why? char* is not a #$*&@!@ string. It's a pointer to
a single character. This isn't C.

You are correct but i want the final string in char * form and not C++
string
>string somevar = PREFIX + '/' + MIDDLE + '.' + SUFFIX;

or

ostringstream os;
os << PREFIX << "/" << MIDDLE << "." << SUFFIX;
string somevar = os.str();
char* crappy_c_string = new char[somevar.size() + 1];
memcpy(crappy_c_string, somevar.c_str(), somevar.size() + 1];

Sep 27 '06 #12
"shyam" <sh********@gmail.comwrites:
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
program dumped with SIGSEGV.
snprintf is dangerous and flawed, it should not be used in C code, let
alone C++ code.

For C, use snprintf, for C++, use std::stringstream and/or
std::string as other have already suggested.

My bet is that your somevar buffer is to small to hold some of your
resulting formatted strings. That's exactly why that function
shouldn't be used.
I did a bt on the core file at gdb prompt and found that the problem is
with c_str( )
That would surprise me. In the above statement, there is nothing wrong
except the potential buffer overflow. What exactly is gdb telling you?

Regards,

Jens
Sep 27 '06 #13

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

Similar topics

4
by: Ericcson | last post by:
Hi. Compilation of this program is OK : --------------------- #include <string> #include <fstream> using namespace std ; int main() { ofstream myfile; string FileName="Sample";...
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: Alfonso Morra | last post by:
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
4
by: Alex Vinokur | last post by:
I have got two functions: void foo1(char* str) { // Stuff } void foo2(int size) { char* str;
2
by: vichoty | last post by:
Hi, I have a question on the following code: void foo(string p) { const char *cptr; { string str = "Hello" + p; cptr = str.c_str(); }
7
by: matish | last post by:
Hi, in: string s("Hi"); const char* c = s.c_str(); how long will the pointed cstring live? I see that calling delete c or delete c fail, that the cstring survives the end of the scope in...
8
by: puzzlecracker | last post by:
Any ideas what may cause that for the string class to core dump? #0 0x0018de92 in std::string::c_str () from /usr/lib/libstdc++.so.5
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.