2014-04-06 72 views
2

我不能为了我的生活找出发生了什么事。这里的错误,我得到:在qmc类的功能Halton,我已经包括了相关位下方出现malloc.c:2451:sYSMALLOc:声明失败

alloc static vecs 
a.out: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. Aborted (core dumped) 

错误。正如你所看到的那样,第一个打印语句“alloc static vecs”会被执行,但是std::vector<double> H(s);这个语句不会显示,因为它后面的print语句没有执行。现在

,我要指出的是,当我更换声明static std::vector<int> bases = FirstPrimes(s);Haltonstatic std::vector<int> bases = {2,3,5,7,11,13};(RHS中是FirstPrimes()返回数组,只是硬编码),那么就没有错误。

Halton还有更多的功能(它返回一个std::vector),但为了简洁起见我省略了它们。如果有人想尝试自己运行它,我会添加它们,请问!

我使用的是g ++ 4.6和Ubuntu 12.04,编译命令是g++ -std=c++0x scratch.cpp QMC.cpp

主(scratch.cpp):

#include <iostream> 
#include <vector> 
#include "QMC.h" 

int main() { 
    QMC qmc; 

    std::vector<double> halton = qmc.Halton(6,1); 
} 

QMC.h:

#ifndef QMC_H 
#define QMC_H 

#include <iostream> 
#include <cmath>         
#include <vector> 

class QMC { 
public: 
    QMC(); 
    bool isPrime(int n); 
    std::vector<int> ChangeBase(int n, int radix); 
    std::vector<int> NextChangeBase(std::vector<int>& a_in, int radix); 
    double RadicalInverse(std::vector<int>& a, int b); 
    std::vector<int> FirstPrimes(int n); 
    std::vector<double> Halton(int s, int n = 0); 
}; 
#endif 

QMC.cpp:

#include "QMC.h" 

QMC::QMC(){} 

std::vector<double> QMC::Halton(int s, int n) { 
    static std::vector<std::vector<int> > newBases(s); 
    static std::vector<int> bases = FirstPrimes(s); 

    /* replacing the statement immediately above with 
    static std::vector<int> bases = {2,3,5,7,11,13}; fixes it */ 

    std::cout << "alloc static vecs \n"; 

    std::vector<double> H(s); 

    std::cout << "alloc H \n"; 

    // ...there's more to this function, but the error occurs just above this. 
} 

std::vector<int> QMC::FirstPrimes(int n) { 

    std::vector<int> primes(n); 
    primes[0] = 2; 

    int testNum = 3; 

    for (int countOfPrimes = 1; countOfPrimes <= n; ++countOfPrimes) { 
    while (isPrime(testNum) == false) 
     testNum = testNum + 2; 

    primes[countOfPrimes] = testNum; 
    testNum = testNum + 2; 
    } 

    return primes; 
} 

bool QMC::isPrime(int n) { 
    if (n == 1) return false; // 1 is not prime                         
    else if (n < 4) return true; // 2 & 3 are prime                        
    else if (n % 2 == 0) return false; // even numbers are not prime                    
    else if (n < 9) return true; // 5 & 7 are prime                         
    else if (n % 3 == 0) return false; // multiples of 3 (> 3) are not prime                  
    else 
    { 
     int r = floor(sqrt((double)n)); 
     int f = 5; 

     while (f <= r) 
     { 
      if (n % f == 0) return false; 
      if (n % (f + 2) == 0) return false; 
      f += 6; 
     } 

     return true; 
    } 
} 
+0

@MattMcNabb只是这样做了,是的,我仍然得到错误:/ – bcf

+0

当你写一个循环的条件来保持循环,并且你发现自己使用<=时,再看看循环。 'countOfPrimes <= n'。当我看到这个,并且我看到countOfPrimes或者变量碰巧被用作索引时,它就会引发一个红旗。 – PaulMcKenzie

回答

2

FirstPrimes具有缓冲器溢出。相关线:

std::vector<int> primes(n); 
primes[0] = 2; 

for (int countOfPrimes = 1; countOfPrimes <= n; ++countOfPrimes) 
    primes[countOfPrimes] = testNum; 

对于尺寸n的载体,本valud指数0通过n-1。在最后一次循环迭代中,您可以进行超出界限的访问。

我建议改变[ ].at(),以及修复逻辑错误。如果您碰巧用n == 0调用此函数,这也可以避免麻烦。

+0

不错!好看。奇怪的是,这个错误刚刚开始,我已经使用了这个代码约一个月没有问题。我有兴趣知道为什么! – bcf

+0

只要运气好后在内存中发生的事情,我猜 –

+0

@bcf - 当你超出缓冲区时,任何事情都可能发生。该程序可能会连续运行3到4次,然后再次运行时崩溃。或者在您的机器上运行一千次,但是一旦您在另一台机器上运行它,就会立即崩溃。 – PaulMcKenzie