iphone - C 中分配内存的问题

标签 iphone objective-c c memory-management

编辑2

在我的其他 SO question 中查找有关此问题的更多信息。

编辑 1

我刷新了这篇文章,因此内容可能与 Alexey、Hicham、Jonathan 和 Mat 的评论不一致。

下面的代码使用 this FFT 来协助起始检测。当该方法被调用一次时,一切正常并且我得到了一个很好的值日志。当第二次调用该方法时,我得到 nan 或垃圾。有什么想法吗?

{
    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];    
}

第一次调用方法时的日志:

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

第二次调用消息时的日志。

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); 

这段代码对我来说有点奇怪。这可能会更好。

 OnsetsDS  *ods = malloc(sizeof OnsetsDS); 

我知道如果您在 C 或 C++ 中执行此操作,*ods 可能会执行许多操作之一,这一切都取决于您的编译器。它可能还没有初始化,指向NULL,指向带有垃圾数据的随机内存地址,甚至其他东西。

你也可以做类似的事情

 OnsetsDS ods;

在很多情况下,至少在 C++ 中,只需将 &ods 与变量一起传递即可。我承认我仍在学习 Objective-C 的过程中。

关于iphone - C 中分配内存的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11922289/

相关文章:

ios - 在 iOS( objective-c )中单击 Button 时,如何在 UITableView 中获取特定的 JSON 数据?

objective-c - 使用 Objective-C 的 Mac OS X 守护进程 - launchd

ios - 完成模态视图后重新加载 UICollectionView

c - C如何保护指针内存?

c - Flex Bison 计算器不打印结果

iphone - UITextField焦点

iphone - 视网膜中的ClipToMask

Python C 扩展关键字参数

ios - 乘以文本字段错误?

ios - 为什么这个方法会抛出异常?