无法找出C程序崩溃

时间:2018-08-06 10:51:06

标签: c

在为FIR过滤器编写C程序时,面临程序崩溃的问题。 奇怪的是在调试模式下,没有发现任何奇怪的现象。代码正常运行。 但是在运行时崩溃。使用的IDE是代码块。

请在下面找到代码段

main.c

#include "common.h"
#include "carrier_NCO_generation.h"
#include "lfsr.h"

int main()
{
    int *nco_out = NULL,*noise_out = NULL,*carrier_gen_I = NULL,*carrier_gen_Q = NULL,*inp_data_I = NULL,*inp_data_Q = NULL;
    int *complex_product_I = NULL,*complex_product_Q = NULL,*FIR_I_Data = NULL,*FIR_Q_Data = NULL;
    int i;

    nco_out             = (int *)malloc(SAMPLES*sizeof(int));
    noise_out           = (int *)malloc(SAMPLES*sizeof(int));
    carrier_gen_I       = (int *)malloc(SAMPLES*sizeof(int));
    carrier_gen_Q       = (int *)malloc(SAMPLES*sizeof(int));
    inp_data_I          = (int *)malloc(SAMPLES*sizeof(int));
    inp_data_Q          = (int *)malloc(SAMPLES*sizeof(int));
    complex_product_I   = (int *)malloc(SAMPLES*sizeof(int));
    complex_product_Q   = (int *)malloc(SAMPLES*sizeof(int));
    FIR_I_Data          = (int *)malloc(SAMPLES*sizeof(int));
    FIR_Q_Data          = (int *)malloc(SAMPLES*sizeof(int));

    fileRead(1,SAMPLES,inp_data_I,"rf_sam_i.txt",1);
    fileRead(1,SAMPLES,inp_data_Q,"rf_sam_q.txt",1);


    carrier_NCO(nco_out);
    lfsr(noise_out);
    carrier_generation(nco_out,noise_out,carrier_gen_I,carrier_gen_Q);
    frequency_transpose(carrier_gen_I,carrier_gen_Q,inp_data_I,inp_data_Q,complex_product_I,complex_product_Q);

    fir(complex_product_I,complex_product_Q,FIR_I_Data,FIR_Q_Data);

    /*
    File Record
    */

    fileRecord(1,SAMPLES,nco_out,"Carrier_NCO_out.txt",0);
    free(nco_out);
    fileRecord(1,SAMPLES,noise_out,"Carrier_NOISE_out.txt",0);
    free(noise_out);
    fileRecord(1,SAMPLES,carrier_gen_I,"Carrier_gen_I_out.txt",0);
    free(carrier_gen_I);
    fileRecord(1,SAMPLES,carrier_gen_Q,"Carrier_gen_Q_out.txt",0);
    free(carrier_gen_Q);
    fileRecord(1,SAMPLES,complex_product_I,"Freq_Trans_I_out.txt",0);
    free(complex_product_I);
    fileRecord(1,SAMPLES,complex_product_Q,"Freq_Trans_Q_out.txt",0);
    free(complex_product_Q);
    fileRecord(1,SAMPLES,FIR_I_Data,"FIR_I_Data_out.txt",0);
    free(FIR_I_Data);
    fileRecord(1,SAMPLES,FIR_Q_Data,"FIR_Q_Data_out.txt",0);
    free(FIR_Q_Data);

    return 0;
}

问题出在FIR过滤器中,代码在下面给出

FIR.C

#include "fir.h"

void array_multiplication(int *array_data, int coefficients[], int size)
{
    int i = 0;
    for(i = 0; i<size; i++)
    {
        array_data[i] = array_data[i]*coefficients[i];
    }
}

int array_addition(int *array_data, int size)
{
    int i = 0,sum = 0;
    for(i = 0; i<size; i++)
    {
        sum = sum + array_data[i];
    }
    return sum;
}

void shift_array_elements(int *array_data, int size)
{
    int i = 0;
    for(i=size;i>0;i--)
    {
        array_data[i] = array_data[i-1];
    }
}

int symmetrical_rounding_without_bit_extension_8_bit_fir(int number)
{
     int bit_7 = 0,neg_flag = 0,max_negtve = -1048576, max_postve = 1048448;
     int mask_number = 2097024;

     //printf("number at input = %d\n",number);

    if((number < 0)&&(number <= -1048576))
    {
        return max_negtve;
    }
    else if((number > 0)&&(number >= 1048448))
    {
        return max_postve;
    }
    else if(number < 0)
    {
        number = ((number) * (-1));
        neg_flag = 1;

    }

    bit_7 = bitExtracted(number,1,7);

    number = number & mask_number;

    if(bit_7)
    {
        number = number + 128;
    }
    if(neg_flag == 1)
    {
        number = (number * (-1));
    }
    return number;
}

int symmetrical_saturation_on_8_th_bit(int number)
{
    int mask_with = 0x7F,neg_flag = 0,max_negtve = -127, max_postve = 128;

    if((number < 0)&&(number <= max_negtve))
    {
        return max_negtve;
    }
    else if((number > 0)&&(number >= max_postve))
    {
        return max_postve;
    }
    else if(number == 0)
    {
        return 0;
    }
    else if(number < 0)
    {
        number = ((number) * (-1));
        neg_flag = 1;
    }

    number = number & mask_with;
    if(number == 0)
    {
        number = max_postve;
    }
    if(neg_flag == 1)
    {
        number = (number * (-1));
    }
    return (int)number;
}

void fir(int *complex_I_data,int *complex_Q_data,int *I_data_out,int *Q_data_out)
{
    int i = 0,j =0,k = 0,real_sum = 0,imag_sum = 0;
    int *real_part = NULL,*imag_part = NULL,*inp_real_part = NULL,*inp_imag_part = NULL;

    real_part = (int *)malloc(FIR_COEFFICIENT * sizeof(int));
    imag_part = (int *)malloc(FIR_COEFFICIENT * sizeof(int));
    inp_real_part = (int *)malloc(FIR_COEFFICIENT * sizeof(int));
    inp_imag_part = (int *)malloc(FIR_COEFFICIENT * sizeof(int));

    //Initialize to 0.
    for(i =0; i<FIR_COEFFICIENT; i++)
    {
        real_part[i] = 0;
        imag_part[i] = 0;
        inp_real_part[i] = 0;
        inp_imag_part[i] = 0;
    }

    for(i = 0; i<SAMPLES; i++)
    {

        inp_real_part[0] = complex_I_data[i];
        inp_imag_part[0] = complex_Q_data[i];

        for(j = 0; j<FIR_COEFFICIENT; j++)
        {
            real_part[j] = inp_real_part[j];
            imag_part[j] = inp_imag_part[j];
        }

        //Array multiplication
        array_multiplication(real_part,precumul_fir_coeff_i,FIR_COEFFICIENT);
        array_multiplication(imag_part,precumul_fir_coeff_q,FIR_COEFFICIENT);

        //sum of array elements
        real_sum = array_addition(real_part,FIR_COEFFICIENT);
        imag_sum = array_addition(imag_part,FIR_COEFFICIENT);

        real_sum = symmetrical_rounding_without_bit_extension_8_bit_fir(real_sum);
        imag_sum = symmetrical_rounding_without_bit_extension_8_bit_fir(imag_sum);


        I_data_out[i] = symmetrical_saturation_on_8_th_bit(real_sum);
        Q_data_out[i] = symmetrical_saturation_on_8_th_bit(imag_sum);


        //shifting array elements
        if((i < (SAMPLES-1)))
        {
            //printf("Reached\n");
            shift_array_elements(inp_real_part,FIR_COEFFICIENT);
            shift_array_elements(inp_imag_part,FIR_COEFFICIENT);
        }
    }
    real_part = NULL;
    imag_part = NULL;
    inp_real_part = NULL;
    inp_imag_part = NULL;

    free(real_part);
    free(imag_part);
    free(inp_real_part);
    free(inp_imag_part);
}

在FIR.C中,Free引发错误,因此将变量分配为NULL。让我知道这是否是错误的。 另外,SAMPLES = 100。

请让我知道我要去哪里了。

谢谢。

1 个答案:

答案 0 :(得分:1)

代码不完整,因此我无法验证,但是函数shift_array_elements似乎超出范围:

void shift_array_elements(int *array_data, int size)
{
    int i = 0;
    for(i=size;i>0;i--)
    {
        array_data[i] = array_data[i-1];
    }
}

从i = size开始,这是从fir()函数调用时超出的范围:

    if((i < (SAMPLES-1)))
    {
        //printf("Reached\n");
        shift_array_elements(inp_real_part,FIR_COEFFICIENT);
        shift_array_elements(inp_imag_part,FIR_COEFFICIENT);
    }

这里的最后一个元素是inp_real_part[FIR_COEFFICIENT-1],但是shift_array_elements将分配给inp_real_part[FIR_COEFFICIENT]

一般提示

  • 使用调试器(gdb)找出程序崩溃的位置。使用-g -O2进行编译并通过gdb运行exectuable,以从崩溃中获取堆栈跟踪
  • 如果分配失败,
  • malloc将返回NULL。使用malloc/free之前,请务必先检查返回的值。