James Kanze
9/25/2008 9:11:00 AM
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