简体   繁体   English

Bad_alloc问题

[英]Bad_alloc problem

my program throws a std::bad_alloc. 我的程序抛出一个std :: bad_alloc。 After debugging it, I found out it is thrown in code 调试之后,我发现它被抛出了代码

curFinalBucket->points.push_back(p);

where points is a vector<PPointT> . 其中点是一个vector<PPointT> Before the code line, curFinalBucket is initialized as follows. 在代码行之前,将curFinalBucket初始化如下。

PFinalBucket curFinalBucket;
curFinalBucket = (FinalBucket*)malloc(sizeof(FinalBucket));

Strange thing is that if I simply put the above code in main , no exception. 奇怪的是,如果我只是将上面的代码放在main ,也不例外。 But when I put it as follows, 但是当我这样说的时候,

void mergeBucket(map<BucketT, vector<PPointT>, Comp> *pMap, IntT numFinalBuckets)
{
...
PFinalBucket curFinalBucket;
curFinalBucket = (FinalBucket*)malloc(sizeof(FinalBucket));
curFinalBucket->points.push_back(p);
}

int testLoadBalancedLSH(IntT num_fbuckets, RealT avgNumPossessedTerms, IntT np, IntT d, char* dataFile)
{
...
mergeBucket(&mapstore, num_fbuckets);
}

int main(int nargs, char **args) {
...
testLoadBalancedLSH(atoi(args[1]), 0.01 * atoi(args[2]), atoi(args[2]), atoi(args[3]), args[4]);
}

it will throw the exception in question. 它将引发有问题的异常。 Any ideas what this could be all about? 有什么想法可能是关于什么的吗? thanks in advance. 提前致谢。

I think the problem is you are creating curFinalBucket with malloc. 我认为问题是您正在使用malloc创建curFinalBucket。 This does not call the constructor so the internal vector<PPointT> is not initialized when you try to use it. 这不会调用构造函数,因此在尝试使用内部vector<PPointT>不会对其进行初始化。

I guess FinalBucket is something like: 我想FinalBucket是这样的:

 class FinalBucket{
    public:
       vector<PPointT> points;
 }

points needs an initialization that is not happening because FinalBucket constructor is not being called and vector<PPointT> constructor is not being called either. points需要进行初始化,因为不会调用FinalBucket构造函数,也不会调用vector<PPointT>构造函数。

You are just allocating memory, but you need the constructors to be called for initialization to take place. 您只是在分配内存,但是需要调用构造函数以进行初始化。

The only way around this is to use new to allocate FinalBucket. 解决此问题的唯一方法是使用new分配FinalBucket。

PFinalBucket curFinalBucket;
curFinalBucket = new FinalBucket();
curFinalBucket->points.push_back(p);

(FinalBucket*)malloc(sizeof(FinalBucket)); allocates memory for an object of the size of a FinalBucket , but it doesn't actually create a final bucket. FinalBucket大小的对象分配内存,但实际上并没有创建最终存储桶。 You should use new FinalBucket() for this. 您应该new FinalBucket()使用new FinalBucket()

Also, as a note on style, hiding pointers behind macros/typedefs like PFinalBucket makes your code harder to read. 另外,作为样式的注释,在PFinalBucket类的宏/ typedef之后隐藏指针会使您的代码更难阅读。 Just write FinalBucket * instead, it's much clearer. 只需编写FinalBucket *FinalBucket *清楚了。

You should use new FinalBucket to allocate dynamic memory instead of malloc. 您应该使用新的FinalBucket分配动态内存,而不是malloc。 The new operator will call the constructor of FinalBucket , in which vector which be initialized. 新的运算符将调用FinalBucket的构造函数 ,在其中初始化了向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM