问题描述
我面临一个非常奇怪的问题.
我正在使用向量类来存储数据.在我测试过的所有系统中,
我的程序运行正常,但Windows 7 32位系统除外.
Visual Studio通过"free.c"的某些功能提供崩溃点
我已经验证我正在将正确的数据传递给push_back函数调用.
我试图将函数调用放在try catch块中.
但这也没有用.
任何关于撞车原因的想法都值得欢迎.
一个解决方案会让我开心不已.
我的代码是这样的
I am facing a very odd problem.
I am using vector class to store data. In all the system I have tested,
my program working fine except a Windows 7 32 bit system.
Visual studio giving the crash point in some function of "free.c"
I have verified I am passing proper data to the push_back function call.
I have tried to put the function call in a try catch block.
But that also didn''t worked.
Any kind of idea on the reason of the crash is welcome.
A solution will make me more than happy.
My code is something like this
vector<DEV_INFO> vInfo;
...
....
DEV_INFO stDevInfo; // User defined structure
FillDevInfo(&stDevInfo); // User defined function call
...
...
vInfo.push_back(stDevInfo); // This line crashing
推荐答案
class AClass
{
//details don't matter, here
};
struct DEV_INFO
{
AClass *p;
DEV_INFO() {} //Note: no p initialization. Default implementation is like that
~DEV_INFO() { delete p; }
DEV_INFO(const DEV_INFO& a)
{ p = a.p? new AClass(*a.p): 0; }
DEV_INFO& operator=(const DEV_INFO& a)
{ delete p; a.p? p = new AClass(*a.p): 0; }
};
在这种情况下,赋值复制和析构函数假定p
指针为null或有效.
但是默认构造函数不为其提供任何初始化.
在许多系统上,动态分配的内存在被分配给程序之前被初始化为零",因此p被incidetaly
设置为NULL.但是其他编译器(尤其是在调试版本中)会用随机数据预填充指针.因此delete p
将更加值得信赖.
可以使用
正确纠正此样本
In this situation, assignment copy and destructor assume that the p
pointer is either null or valid.
But the default constructor doesn''t provide any initialization for it.
On many systems, dynamic allocated memory is initialized to "zero" before it is given to the program, so p is incidetaly
set to NULL.
But other compiler (especially in debug release) pre-fill the pointer with random data. So delete p
will be anymore trustable.
This sample can be properly corrected with
DEV_INFO::DEV_INFO() :p() {}
因此给p一个空值.
另一个可能的问题是default-ctor,未实现复制和分配(因此p,只是被复制,但是所有复制都指向相同的地址),而dtor会删除p".
您没有提供有关结构行为方式的足够信息.但是这些是应该首先研究的东西.
thus giving to p a null value.
Another possible problem can be default-ctor, copy and assign not implemented (so p, is simply copyed, but all copy point to a same address) and dtor does "delete p".
You didn''t give enough information about the way your structure behaves. But those are the things that should be first investigated.
这篇关于push_back方法崩溃.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!