利用GDB:C++开发类对象创建
考虑下面的代码,我已经写了:利用GDB:C++开发类对象创建,c++,gdb,C++,Gdb,考虑下面的代码,我已经写了: #include <iostream> #include <cstring> #include <cstdlib> using namespace std; class employee{ int ID; char* name; public: employee(int id,const char* ptr); ~employee(); int getID(); char* getName(); };
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class employee{
int ID;
char* name;
public:
employee(int id,const char* ptr);
~employee();
int getID();
char* getName();
};
employee::employee(int id,const char* ptr):ID(id){
name= (char*) malloc((strlen(ptr)+1)*sizeof(char));
strcpy(name,ptr);
cout << "Employee with ID : " << ID << " Name : " << name << " constructed." << endl;
}
employee::~employee(){
cout << "Employee with ID : " << ID << " Name : " << name << " destructed." << endl;
free(name);
name=NULL;
}
int employee::getID(){return ID;}
char* employee::getName(){return name;}
int main()
{
employee Ob1(145645,"Edward Collins");
cout << "Employee Details:" << endl;
cout << "ID: " << Ob1.getID() << " Name : " << Ob1.getName() << endl;
return 0;
}
现在我试图找出存储Ob1、Ob1.ID、Ob1.name、Ob1.getID、Ob1.getName的地址:
$ gdb GDB
(gdb) break 39
Breakpoint 1 at 0x4012f5: file GDB.cpp, line 39.
(gdb) run
Breakpoint 1, main () at GDB.cpp:39
39 employee Ob1(145645,"Edward Collins");
(gdb) s
employee::employee (this=0x22abf8, id=145645, ptr=0x4020b8 <_data_start__+184> "Edward Collins") at GDB.cpp:21
21 employee::employee(int id,const char* ptr):ID(id){
(gdb)
所以基本上我可以看到ID的地址;但我想找出构造对象的类成员的所有地址,并想在gdb的帮助下探索如何在内存中构造类对象,即地址Ob1、Ob1.ID、Ob1.name、Ob1.getID、Ob1.getName
有人能帮忙吗?为什么要查看这些东西的地址?若要显示类成员的值,请尝试打印*此。
$ gdb GDB
(gdb) break 39
Breakpoint 1 at 0x4012f5: file GDB.cpp, line 39.
(gdb) run
Breakpoint 1, main () at GDB.cpp:39
39 employee Ob1(145645,"Edward Collins");
(gdb) s
employee::employee (this=0x22abf8, id=145645, ptr=0x4020b8 <_data_start__+184> "Edward Collins") at GDB.cpp:21
21 employee::employee(int id,const char* ptr):ID(id){
(gdb)
Breakpoint 1, main () at GDB.cpp:39
39 employee Ob1(145645,"Edward Collins");
(gdb) s
employee::employee (this=0x22abf8, id=145645, ptr=0x4020b8 <_data_start__+184> "Edward Collins") at GDB.cpp:21
21 employee::employee(int id,const char* ptr):ID(id){
(gdb) s
22 name= (char*) malloc((strlen(ptr)+1)*sizeof(char));
(gdb) s
23 strcpy(name,ptr);
(gdb) s
24 cout << "Employee with ID : " << ID << " Name : " << name << " constructed." << endl;
(gdb) print &ID
$1 = (int *) 0x22abf8
(gdb) x/d 0x22abf8
0x22abf8: 145645
(gdb) print &name
$2 = (char **) 0x22abfc
(gdb) x/d 0x22abfc
0x22abfc: 537103464
(gdb) info locals
No locals.
(gdb)