设为首页 加入收藏

TOP

花非花--记vs2010 c++调试问题
2014-11-23 21:42:26 来源: 作者: 【 】 浏览:8
Tags:花非花 vs2010 调试 问题

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

[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处错误,殊不知是未分配内存区域造成的后果。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇uva 10916 Factstone Benchmark(.. 下一篇UVa 111: History Grading

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·【C语言】动态内存管 (2025-12-27 06:23:20)
·C语言中的内存管理 - (2025-12-27 06:23:16)
·C语言指南:C语言内 (2025-12-27 06:23:14)
·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)