花非花--记vs2010 c++调试问题

2014-11-23 21:42:26 · 作者: · 浏览: 9

来一道简化过的小菜
代码:

[cpp]
#include

using namespace std;

class test
{
public:
void f()
{
cout << "111" << endl;
}
};


int main()
{
test *p = (test *)0x123;
p->f();
}

#include

using namespace std;

class test
{
public:
void f()
{
cout << "111" << endl;
}
};


int main()
{
test *p = (test *)0x123;
p->f();
}


环境:

vs2010 + c++


结果:
终端显示:111


说明:
实验很简单,但是,可能你会觉得不过如此嘛,在编译的时候编译器根据成员函数名来确定函数入口点,而不是真的需要一个对象。


再来一个麻辣系
代码:

[cpp]
#include
#include // new
using namespace std;

class test
{
public:
void f()
{
//cout << "111" << endl;// new
datas_.push_back(1);//new

}

private:
vector datas_;// new
};


int main()
{
test *p = (test *)0x123;
p->f();
}

#include
#include // new
using namespace std;

class test
{
public:
void f()
{
//cout << "111" << endl;// new
datas_.push_back(1);//new

}

private:
vector datas_;// new
};


int main()
{
test *p = (test *)0x123;
p->f();
}


环境:

一致


结果:
崩啦


总结:
自认为是STL的vector处错误,殊不知是未分配内存区域造成的后果。