core dump due to string::c_str ()

P

puzzlecracker

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
 
J

Juha Nieminen

puzzlecracker said:
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.
 
R

red floyd

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.
 
P

puzzlecracker

  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?
 
P

puzzlecracker

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?


It works quite well.

V

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

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

James Kanze

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.
 
J

Juha Nieminen

puzzlecracker said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top