473,833 Members | 2,116 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
12 5286
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

ostringstrea m 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********@gma il.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::stringstre am 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
26654
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
11615
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
3098
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
3670
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
8189
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
2372
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
6325
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
9796
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
10782
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
10500
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...
1
10543
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7753
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
5624
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
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4422
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
3972
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.