more about gdb
My question about gdb is that I hope it can provide more details.For example, we have a program like this:
#include <stdio.h>
main( ) {
char* a=new char[100];
a="I love music."; //line 3
printf("%s\n",a);
delete [] a; //line 5
}
and it is easy to find that this program has some problem. gcc can pass it, but after running, the program gives out "segmentation fault". Right, line 3 is where the problem is in.
In my opinion, in line 3, compiler will generate a temporary object which holds "I love music".
and compiler will then give the address of this temporary object to char* a. This is terrible.
First, this temporary object is not dynamicly created, so it can not be destroyed by "delete".
Second , the resource generated by "new char[100]" may never be free, so this cause memory leak. The first problem is easy to deal with. Just delete line 5 is ok. But this will not help dealing with the second problem. Because the true problem happens in line 3. If gdb can provide more details about program , such as show us the generation of temporary object, we can deal with the programs more effectively.
Another program:
class KApplication : public QApplication, public KInstance
{
KApplication::KApplication( ) : QApplicatin( ), KInstance( ) {
.......//do something
} //constructor end
}
So if we write "KApplication app = new Kapplication( );" , we know what will happen. First, QApplication::QApplication( ) and KInstance::KInstance( ) will be called. And then,
KApplication::Kapplication it self will be called. (we not think about _new(size_t size ) which will malloc memory for our object) . Soon before , I can see all this process happen by "step" in gdb. But now, I can not see the process of QApplication::QApplication( ) and KInstance::KInstance( ). I don't know what has happened to my gdb. And even more, few days ago,
when I use "step" to watch my program, I can even see the process of constructor and destructor of temporary object. And now, nothing. Can you give some hint ?
really thanks. If you can tell me some forum about gdb, it will also help.
Jiang
|