gpt4 book ai didi

c++ - 使用 vector 时 sysmalloc 断言失败

转载 作者:行者123 更新时间:2023-11-30 04:09:08 25 4
gpt4 key购买 nike

我正在运行这个 c++ 程序,它不断给我 sysmalloc assertion failed 错误。我正在使用 g++-4.8。

# include <iostream>
# include <complex>
# include <vector>
# include <stdlib.h>
# include <time.h>
# include <climits>
# include <cstdio>
#define getRand(x,y) (rand()%((y)-(x)+1)+(x))
using namespace std;


typedef complex<double> Complex;

typedef struct compositeSignal{
int *frequency;
int *amplitude;
int num;

compositeSignal(int n)
{
frequency = new int(n);
amplitude = new int(n);
num = n;

for(int i=0;i<num;i++)
{
amplitude[i] = getRand(1,100);
cout<<amplitude[i]<<" ";
frequency[i] = getRand(100,600);
cout<<frequency[i]<<endl;
}

}


double getVal(double t)
{
double val = 0;
for(int i = 0;i<num;i++)
val += amplitude[i]*sin(2*3.142*frequency[i]*t);
return val;
}
}compositeSignal;


int main()
{
srand(time(NULL));
int numOfSig = 5;
compositeSignal sig1(numOfSig);// sig2(numOfSig); //define and initialize 2 composite signals


vector<int> result(10);

}

错误:

a.out: malloc.c:2369: sysmalloc: Assertion `(old_top == (((mbinp
tr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offseto
f (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned l
ong) (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) && ((uns
igned long)old_end & pagemask) == 0)' failed.
Aborted

错误是在最后一行分配 vector result 时。如何避免此错误?

最佳答案

你有未定义的行为,因为你只为每个 frequencyamplitude 分配一个 int 然后你试图访问amplitude[1] 等等。 new int(n) 语法将分配单个 int 并用值 n 初始化它。您正在寻找的是 new int[n],它将分配 arrayn int .

关于c++ - 使用 vector 时 sysmalloc 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21467981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com