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 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
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.
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.
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();
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();
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.
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?
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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());
|
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...
|
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
|
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?????
|
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
| |
by: Alex Vinokur |
last post by:
I have got two functions:
void foo1(char* str)
{
// Stuff
}
void foo2(int size)
{
char* str;
|
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();
}
|
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
|
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
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |