- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我这辈子都搞不清楚到底发生了什么。这是我得到的错误:
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)
错误发生在函数Halton
在类里面qmc
,我已经在下面包含了相关的部分。如您所见,第一个打印语句“alloc static vecs”执行,但语句std::vector<double> H(s);
似乎没有,因为紧随其后的打印语句不会执行。
现在,我应该在替换语句 static std::vector<int> bases = FirstPrimes(s);
时提到这一点在Halton
与 static 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
的 vector ,有效索引为 0
到 n-1
。在最后一次循环迭代中,您进行了越界访问。
我建议将 [ ]
都更改为 .at( )
,并修复逻辑错误。如果您碰巧使用 n == 0
调用此函数,这也可以防止出现问题。
关于c++ - malloc.c :2451: sYSMALLOc: Assertion . .. 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22889175/
我希望有人能帮助我理解我哪里出了问题。我正在实现一个程序来检查拼写正确性。在此过程中,我使用 trie 数据结构将字典文本文件加载到内存中以检查单词。 总体而言,它似乎按预期运行,但在加载尽可能长的单
我在运行 C 程序时遇到以下 sysmalloc 错误。 malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *)
sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size)
我编写了一个代码来将新成员添加到列表中。当我添加两个成员时它工作正常。但是,一旦我添加第三个并编译并运行代码,就会出现错误。代码及错误如下: #include #include struct list
我已经为这个错误苦苦挣扎了一段时间,但我不知道出了什么问题。这是代码: //the code for the function that is being called //charset is a
我正在尝试编写一个函数,用于搜索模式的所有出现并返回文件中与该模式匹配的偏移量数组。我想使用 realloc 动态增长返回的数组,但出现 SYSMALLOC 断言错误。有时,如果我使用不同的搜索模式,
我在使用 opencv 时遇到 sysmalloc 错误。当我调试时,我发现错误发生在这里: sm = cv::Mat::zeros(h,w,img.type()); 其中h和w分别是img行和w列。
#include #include char* text1 = "This is a string."; char* text2 = "Yet another thing."; void copy
我的 C 程序出现以下消息错误: a.out: malloc.c:2369: sysmalloc: Assertion `(old_top == (((mbinptr)(((char *) &((av
下面的 C 代码在我的 mac OS X 环境中运行良好,但如果我尝试在 ubuntu 环境中运行此代码,每当我输入偶数个输入(例如“1 2”)时,我都会遇到 malloc 断言失败,但奇数输入“1
我在 C 程序上收到一条 SYSMALLOc 错误消息,我相信此错误消息与我使用过的 malloc 相关,而且我通过在 malloc 周围放置 printf 语句发现了这一点,并且它导致了问题。我看不
我正在运行这个 c++ 程序,它不断给我 sysmalloc assertion failed 错误。我正在使用 g++-4.8。 # include # include # include #
/* Dynamic Programming implementation of LCS problem */ #include #include #include #include using na
EDIT: For clarity's sake, I'll leave the question as it is. The problem seems to be that the Kameleo
我正在编写一个名为 Process 的非常简单的结构,乍一看代码似乎已正确实现,但在测试我的代码时,程序似乎一直在崩溃,无论是由于 sysMalloc 断言失败还是由于 double free() 错
我的服务器守护进程在大多数机器上运行良好,但在我得到的一台机器上: malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((ch
我的函数如下: void Insert_ldb(int t){ struct node_ldb *temp_ldb1,*lastnode_ldb; temp_ldb1=root_ldb
我这辈子都搞不清楚到底发生了什么。这是我得到的错误: alloc static vecs a.out: malloc.c:2451: sYSMALLOc: Assertion `(old_top ==
我遇到了一个神秘的错误,我不知道为什么。这段代码在失败之前运行了几次,并且总是在同一点失败。 这是我的代码: assert(size > 0); int* sorted = mallo
对于学校作业,我需要跟踪 Product 类的多个实例。 在我的程序的主要部分,我正在制作一个 vector ,如下所示: std::vector> products; 在我的 main 中的特定条件
我是一名优秀的程序员,十分优秀!