sysmalloc断言使用向量失败

时间:2014-01-30 21:41:33

标签: c++ memory malloc

我正在运行这个c ++程序,它不断给我sysmalloc assertion失败的错误。我正在使用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

错误是在最后一行我分配向量result。如何避免此错误?

1 个答案:

答案 0 :(得分:2)

您有未定义的行为,因为您只为每个intfrequency分配了一个amplitude,然后您尝试访问amplitude[1],依此类推。 new int(n)语法将分配单个int并使用值n对其进行初始化。您要找的是new int[n],它将分配n int数组