[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

core dump due to string::c_str

puzzlecracker

9/24/2008 6:36:00 PM

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
8 Answers

Juha Nieminen

9/24/2008 7:34:00 PM

0

puzzlecracker wrote:
> 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

Yes: You are accessing out of bounds somewhere.

Run your program with valgrind.

red floyd

9/24/2008 8:45:00 PM

0

On Sep 24, 11:35 am, puzzlecracker <ironsel2...@gmail.com> wrote:
> 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

Come on Puzzlecracker, you should know FAQ 5.8 by now.

There is an error on line 42 of your code.

Post a minimal, compileable example that exhibits the behavior in
question.

puzzlecracker

9/24/2008 9:10:00 PM

0

On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> puzzlecracker wrote:
> > 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
>
>   Yes: You are accessing out of bounds somewhere.
>
>   Run your program with valgrind.

Argh, I traced why it happened, and modified my code, removing the
stupidity. Here what I had:

void foo(std::string &str)
{

}


void bar(std::string str)
{
foo(str.c_str());
}


Never used valgrind before. How does it work?

Victor Bazarov

9/24/2008 9:35:00 PM

0

puzzlecracker wrote:
> On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
>> puzzlecracker wrote:
>>> 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
>> Yes: You are accessing out of bounds somewhere.
>>
>> Run your program with valgrind.
>
> Argh, I traced why it happened, and modified my code, removing the
> stupidity. Here what I had:
>
> void foo(std::string &str)
> {
>
> }
>
>
> void bar(std::string str)
> {
> foo(str.c_str());
> }

That shouldn't compile. You're trying to bind a non-const reference to
a temporary object (created from the pointer to char you get by calling
the 'c_str()'). That's not allowed. Begin by dialing up the warning
level on your compiler and possibly by disabling extensions you're not
consciously using.

OTOH, there is nothing in this particular code that would cause the
crash. Are you sure you're not trying to access 'c_str' member using an
invalid reference?

> Never used valgrind before. How does it work?

It works quite well.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

puzzlecracker

9/25/2008 1:51:00 AM

0

On Sep 24, 5:35 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> puzzlecracker wrote:
> > On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> >> puzzlecracker wrote:
> >>> 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
> >> Yes: You are accessing out of bounds somewhere.
>
> >> Run your program with valgrind.
>
> > Argh, I traced why it happened, and modified my code, removing the
> > stupidity. Here what I had:
>
> > void foo(std::string &str)
> > {
>
> > }
>
> > void bar(std::string str)
> > {
> > foo(str.c_str());
> > }
>
> That shouldn't compile. You're trying to bind a non-const reference to
> a temporary object (created from the pointer to char you get by calling
> the 'c_str()'). That's not allowed. Begin by dialing up the warning
> level on your compiler and possibly by disabling extensions you're not
> consciously using.
>
> OTOH, there is nothing in this particular code that would cause the
> crash. Are you sure you're not trying to access 'c_str' member using an
> invalid reference?
>
> > Never used valgrind before. How does it work?
>
> It works quite well.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask

Argh, I was passing string in Foo as a const std::string &

I thought c_str() returning const char *...Hmm

James Kanze

9/25/2008 9:11:00 AM

0

On Sep 25, 3:50 am, puzzlecracker <ironsel2...@gmail.com> wrote:
> On Sep 24, 5:35 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> > puzzlecracker wrote:
> > > On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> > >> puzzlecracker wrote:
> > >>> 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
> > >> Yes: You are accessing out of bounds somewhere.

> > >> Run your program with valgrind.

> > > Argh, I traced why it happened, and modified my code, removing the
> > > stupidity. Here what I had:

> > > void foo(std::string &str)
> > > {
> > > }

> > > void bar(std::string str)
> > > {
> > > foo(str.c_str());
> > > }

> > That shouldn't compile. You're trying to bind a non-const
> > reference to a temporary object (created from the pointer to
> > char you get by calling the 'c_str()'). That's not allowed.
> > Begin by dialing up the warning level on your compiler and
> > possibly by disabling extensions you're not consciously
> > using.

> > OTOH, there is nothing in this particular code that would
> > cause the crash. Are you sure you're not trying to access
> > 'c_str' member using an invalid reference?

> > > Never used valgrind before. How does it work?

> > It works quite well.

> Argh, I was passing string in Foo as a const std::string &

> I thought c_str() returning const char *...Hmm

It does, but there is an implicit conversion from char const* to
std::string.

And as Victor said, there's no reason in the code you posted for
anything not to work. You're probably overwriting memory
somewhere else. Valgrind is free; download it and use it.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Victor Bazarov

9/25/2008 2:15:00 PM

0

puzzlecracker wrote:
> On Sep 24, 5:35 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> puzzlecracker wrote:
>>> On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
>>>> puzzlecracker wrote:
>>>>> 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
>>>> Yes: You are accessing out of bounds somewhere.
>>>> Run your program with valgrind.
>>> Argh, I traced why it happened, and modified my code, removing the
>>> stupidity. Here what I had:
>>> void foo(std::string &str)
>>> {
>>> }
>>> void bar(std::string str)
>>> {
>>> foo(str.c_str());
>>> }
>> That shouldn't compile. You're trying to bind a non-const reference to
>> a temporary object (created from the pointer to char you get by calling
>> the 'c_str()'). That's not allowed. Begin by dialing up the warning
>> level on your compiler and possibly by disabling extensions you're not
>> consciously using.
>>
>> OTOH, there is nothing in this particular code that would cause the
>> crash. Are you sure you're not trying to access 'c_str' member using an
>> invalid reference?
>>
>>> Never used valgrind before. How does it work?
>> It works quite well.
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask
>
> Argh, I was passing string in Foo as a const std::string &

So, is it 'Foo' or 'foo'? Something makes me think that you're not
posting your real code. See FAQ 5.8.

>
> I thought c_str() returning const char *...Hmm

It does.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Juha Nieminen

9/25/2008 2:36:00 PM

0

puzzlecracker wrote:
> Never used valgrind before. How does it work?

You start valgrind giving it your program (and its possible
command-line parameters) as parameter (similarly to how you would use
eg. the "time" command), and it will report about a wide variety of
possible errors in your program.