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

Home Posts Topics Members FAQ

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 5285
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

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

Similar topics

4
26653
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"; myfile.open((FileName).c_str());
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
8187
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
6910
by: Alex Vinokur | last post by:
I have got two functions: void foo1(char* str) { // Stuff } void foo2(int size) { char* str;
2
1983
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
2371
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 witch it is decleard and even delete s does not
8
6320
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
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
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...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.