Quote:
Originally Posted by graemef
I think that what is going on is some optimisation from the compiler. I've played around with it and I think that what is happening is that the compiler realises that the rev object will be returned and so it is placing its location directly where it will be used in memory, in your calling function (in this case main). Because the memory is there it is in a location out of bounds for the local method and so the debugger gets confused.
|
That sounds like a very reasonable explanation, considering how all the member variable values are in the range of the stack addresses!
One thing is a bit odd, though: shouldn't (even?) these kinds of optimizations be disabled with -O0?
Lots of thanks to everybody who has replied, even though I still don't really have a proper
solution; that doesn't matter too much TBH, at least not for this class... If it happens in the future, it's worse, and I'm more interested in learning the cause and if applicable report the bug somewhere, than actually debug this method (which I already have a FreeBSD computer and an OS X computer perfectly capable of doing!).
Edit: Yeah... If I allocate the string on the heap, I get this:
Code:
exscape::string *s = new exscape::string("ABCDEF");
std::cout << "Reversed: " << s->reverse() << std::endl;
(gdb) p rev
$3 = {buf = 0x7fffffffdcc0 "", _length = 6328336, _size = 4215972}
(gdb) p this
$4 = (const exscape::string * const) 0x609010
this = 0x609010, _length = 0x609010.