繁体   English   中英

malloc.c:2451: sSYSMALLOc: 断言...失败

[英]malloc.c:2451: sYSMALLOc: Assertion ... failed

我一生都无法弄清楚发生了什么。 这是我得到的错误:

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)

错误发生在qmc类中的Halton函数中,我在下面包含了相关位。 如您所见,第一个打印语句“alloc static vecs”执行,但语句std::vector<double> H(s); 似乎没有,因为紧随其后的打印语句不会执行。

现在,我应该提到,当我替换语句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;
    }
}

FirstPrimes有缓冲区溢出。 相关线路:

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

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

对于大小为n的向量,值索引为0n-1 在最后一次循环迭代中,您进行了越界访问。

我建议将[ ]都更改为.at( ) ,并修复逻辑错误。 如果您碰巧使用n == 0调用此函数,这也可以防止出现问题。

暂无
暂无

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

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