问题用C分配内存 [英] Problems allocating memory in C

查看:134
本文介绍了问题用C分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑2

查找关于这个问题我在其他 SO质疑。

修改1

我已经刷新这个帖子等于是内容可能不一致与阿列克谢,希沙姆,乔纳森和Mat的意见。

下面code使用 此FFT 协助发作检测。当该方法被调用一次,一切工作正常,我得到一个不错的日志值。当该方法被称为第二次,我得到或垃圾。任何想法?

  {
    的NSLog(@运行发作。);
    NSMutableArray里* mutableArrayOfFlags = [[NSMutableArray里的alloc]初始化];
    OnsetsDS * ODS =的malloc(sizeof的* ODS);
    浮动* odsdata =(浮点*)malloc的(onsetsds_memneeded(ODS_ODF_RCOMPLEX,512,11));
    onsetsds_init(ODS,odsdata,ODS_FFT_FFTW3_HC,ODS_ODF_RCOMPLEX,512,11,44100);
    INT I;
    INT X;
    布尔发作;    对于(I = 0; I&下; vocalBuffer.numFrames; I = I + 512){        //转换声乐INT翻番
        双(* vocalData)[2] =的malloc(2 * 512 * sizeof的(双));
        为(X = 0; X&所述; 512; X ++){
            * vocalData [X] =(双)vocalBuffer.buffer [I + X];
        }        //初始化的malloc双输出
        双(* outPutDoubleFFTData)[2];
        outPutDoubleFFTData =的malloc(2 * 512 * sizeof的(双));
        FFT(512 vocalData,outPutDoubleFFTData);        INT Z者除外;
        //初始化的malloc浮动FFT数据
        浮* floatFFTData;
        floatFFTData =的malloc(512 * sizeof的(浮动));
        对于(Z = 0; z,其中512; Z ++){
            floatFFTData [Z] =(浮点)* outPutDoubleFFTData [Z]。
            如果(我== 512 * 20){
                //的NSLog(@pre POST%F - %F,* vocalData [Z],floatFFTData [Z]);
                的NSLog(@pre POST FLOAT%F - %F - %F,* vocalData [Z],(* outPutDoubleFFTData)[Z],floatFFTData [Z]);            }
        }        发病= onsetsds_process(ODS,floatFFTData);        免费((* outPutDoubleFFTData));
        免费(floatFFTData);
        免费(vocalData);        如果(发病){
            输出(发作 - >%I \\ N,I);
            *的NSNumber整数= [[NSNumber的页头] initWithInt:我]。
            [mutableArrayOfFlags ADDOBJECT:整数];
        }
    }
    免费(消耗臭氧层物质>数据); //或者免费(odsdata),它们指向同样的事情在这种情况下,
    免费(ODS);
    返回[[NSArray的页头] initWithArray:mutableArrayOfFlags];
}

从第一次登录的方法被调用:

 二○一二年十月二十○日11:22:19.625 XX [4125:1903年] pre POST FLOAT 4.000000  -  7979.000000  -  7979.000000
二○一二年十月二十○日11:22:19.628 XX [4125:1903年] pre POST FLOAT 25.000000 - 0.000000 - 861.794861
二○一二年十月二十○日11:22:19.635 XX [4125:1903年] pre POST FLOAT 32.000000 - 861.794875 - 248.516144
二○一二年十月二十○日11:22:19.640 XX [4125:1903年] pre POST FLOAT 22.000000 - 92.284860 - -190.525833
二○一二年十月二十○日11:22:19.645 XX [4125:1903年] pre POST FLOAT 23.000000 - 248.516141 - 37.045593
二○一二年十月二十○日11:22:19.648 XX [4125:1903年] pre POST FLOAT 30.000000 - -33.565115 - 7.444437

从消息被称为第二次登录。

 二○一二年十月二十○日11:22:36.353 XX [4125:3e07] pre POST FLOAT 4.000000  -  7979.000000  -  7979.000000
二○一二年十月二十○日11:22:36.358 XX [4125:3e07] pre POST FLOAT 25.000000 - 53979063281237364484736793729327605401034441222848177467876829146104162439787488863720409331484927794377967278456986000075570355992521879340404128702782598833969629491268820332191001022225312452183861587484411698307560976546539765760.000000 - INF
二○一二年十月二十○日11:22:36.364 XX [4125:3e07] pre POST FLOAT 32.000000 -


  OnsetsDS * ODS =的malloc(sizeof的* ODS);

这code是有点怪我。这可能是更好的。

  OnsetsDS * ODS =的malloc(sizeof的OnsetsDS);

,我知道,如果你用C或C ++这样做,*消耗臭氧层物质可以做许多事情之一完全取决于你的编译器。它可能还没有被初始化,指向NULL,指向与垃圾数据,甚至是别的一个随机的内存地址。

您也可以这样做。

  OnsetsDS消耗臭氧层物质;

和只传递&放大器;在许多情况下,你的变量,至少在C ++中的消耗臭氧层物质。我仍然在学习目标-C,我会承认的过程。

EDIT 2

Find more info about this problem in my other SO question.

EDIT 1

I have refreshed this post and so therefore the content may be incongruent with the comments made by Alexey, Hicham, Jonathan and Mat.

The below code uses this FFT to assist in onset detection. When the method is called once, everything works fine and I get a nice log of values. When the method is called a second time I get nan or junk. Any ideas?

{
    NSLog(@"Running onset.");
    NSMutableArray *mutableArrayOfFlags = [[NSMutableArray alloc] init];
    OnsetsDS  *ods = malloc(sizeof *ods); 
    float* odsdata = (float*) malloc(onsetsds_memneeded(ODS_ODF_RCOMPLEX, 512, 11));
    onsetsds_init(ods, odsdata, ODS_FFT_FFTW3_HC, ODS_ODF_RCOMPLEX, 512, 11, 44100);
    int i;
    int x;
    bool onset; 

    for (i = 0; i < vocalBuffer.numFrames; i=i+512){

        // convert vocal int to double 
        double (*vocalData)[2] =  malloc(2 * 512 * sizeof(double));
        for (x = 0; x < 512; x++){
            *vocalData[x] = (double)vocalBuffer.buffer[i+x]; 
        }

        // init malloc output double 
        double (*outPutDoubleFFTData)[2];
        outPutDoubleFFTData =  malloc(2 * 512 * sizeof(double)); 
        fft(512, vocalData, outPutDoubleFFTData);



        int z;
        // init malloc float fft data
        float *floatFFTData; 
        floatFFTData = malloc(512 * sizeof(float));
        for (z = 0; z < 512; z++){
            floatFFTData[z] = (float)*outPutDoubleFFTData[z];
            if (i==512*20) {
                // NSLog(@"PRE POST %f - %f",*vocalData[z], floatFFTData[z]);
                NSLog(@"PRE POST FLOAT %f - %f - %f",*vocalData[z], (*outPutDoubleFFTData)[z], floatFFTData[z]);

            }
        }

        onset = onsetsds_process(ods, floatFFTData);

        free((*outPutDoubleFFTData));
        free(floatFFTData);
        free(vocalData);

        if (onset){
            printf("onset --> %i\n", i);
            NSNumber *integer = [[NSNumber alloc] initWithInt:i];
            [mutableArrayOfFlags addObject:integer];
        }
    }


    free(ods->data); // Or free(odsdata), they point to the same thing in this case
    free(ods);
    return [[NSArray alloc] initWithArray:mutableArrayOfFlags];    
}

Log from first time method is called:

2012-10-20 11:22:19.625 XX[4125:1903] PRE POST FLOAT 4.000000 - 7979.000000 - 7979.000000
2012-10-20 11:22:19.628 XX[4125:1903] PRE POST FLOAT 25.000000 - 0.000000 - 861.794861
2012-10-20 11:22:19.635 XX[4125:1903] PRE POST FLOAT 32.000000 - 861.794875 - 248.516144
2012-10-20 11:22:19.640 XX[4125:1903] PRE POST FLOAT 22.000000 - 92.284860 - -190.525833
2012-10-20 11:22:19.645 XX[4125:1903] PRE POST FLOAT 23.000000 - 248.516141 - 37.045593
2012-10-20 11:22:19.648 XX[4125:1903] PRE POST FLOAT 30.000000 - -33.565115 - 7.444437

Log from second time message is called.

2012-10-20 11:22:36.353 XX[4125:3e07] PRE POST FLOAT 4.000000 - 7979.000000 - 7979.000000
2012-10-20 11:22:36.358 XX[4125:3e07] PRE POST FLOAT 25.000000 - 53979063281237364484736793729327605401034441222848177467876829146104162439787488863720409331484927794377967278456986000075570355992521879340404128702782598833969629491268820332191001022225312452183861587484411698307560976546539765760.000000 - inf
2012-10-20 11:22:36.364 XX[4125:3e07] PRE POST FLOAT 32.000000 - 

解决方案

 OnsetsDS  *ods = malloc(sizeof *ods); 

This code is a little weird to me. This might be better.

 OnsetsDS  *ods = malloc(sizeof OnsetsDS); 

I know if you did this in C or C++, *ods could be doing one of many things, all depending on your compiler. It might not be initialized yet, pointing to NULL, pointing to a random memory address with junk data, or even something else.

You can also do something like

 OnsetsDS ods;

and just pass &ods with your variables in many cases, at least in C++. I'm still in the process of learning objective-C I'll admit.

这篇关于问题用C分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆