LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-30-2009, 03:35 AM   #16
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352

Rep: Reputation: 129Reputation: 129

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.

I changed the reverse method slightly as follows and I could see rev build up as expected but this time temp was behaving odd.

Code:
string string::reverse(void) const {
if (DEBUG) std::cerr << "In reverse() for string " << this << "(length=" << this->_length << ")" << std::endl;
 
string temp = string();
string rev = string();
 
rev.alloc(this->_length + 1);
 
for (size_t i=0; i<this->_length; i++) {
rev.buf[this->_length-i-1] = this->buf[i];
if (DEBUG) std::cerr << "In reverse() for string " << rev.buf[this->_length-i-1] << std::endl;
}
 
rev._length = this->_length;
temp = rev;
return temp;
}
 
Old 11-30-2009, 12:53 PM   #17
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by graemef View Post
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.

Last edited by exscape; 11-30-2009 at 01:01 PM.
 
  


Reply

Tags
c++, debugging, gdb


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Garbage Collection in C : Local variables question duryodhan Programming 13 12-04-2006 07:16 AM
shared libraries, dlopen and static member variables? (C/C++) Thinking Programming 2 12-19-2005 12:55 AM
normal gdb and spec gdb for kgdb Igor007 Linux - Newbie 1 09-23-2005 01:41 PM
console font is just showing garbage plisken Linux - General 3 01-11-2005 12:51 PM
detecting out of bound gdb convenience variables Hano Programming 1 07-16-2004 03:01 PM


All times are GMT -5. The time now is 04:37 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration