gpt4 book ai didi

ios - Objective-C:单步执行 double* 数组时 EXC_BAD_ACCESS code=2

转载 作者:行者123 更新时间:2023-11-29 02:57:07 25 4
gpt4 key购买 nike

在我的 init 中,我设置了以下内容:

- (id)init
{
if (self = [super init])
{
// Time-Domain Wavelet pitch detection setup
self.bufferCapacity = 2048;
self.index = 0;
dataBuffer = (SInt16*)malloc(self.bufferCapacity * sizeof(SInt16));
dywapBuffer = (double*)malloc(self.bufferCapacity * sizeof(double));
dywapitch_inittracking(&pitchtracker);
}
return self;
}

在回调中,我尝试执行以下操作:

    SInt16 *dataBuffer = THIS->dataBuffer;
double *dywapBuffer = THIS->dywapBuffer;
for (int i = 0; i < THIS.bufferCapacity; i++)
{
double* doublePointer = dywapBuffer + (i * sizeof(double));
SInt16* intPointer = dataBuffer + (i * sizeof(SInt16));
*doublePointer = (double)(*intPointer);
}
double frequency = dywapitch_computepitch(&THIS->pitchtracker, dywapBuffer, 0, frames);

但是,当 for 循环达到 i = 256 时,应用程序在尝试分配 doublePointer 时会崩溃,并显示 EXC_BAD_ACCESS code=2。任何人都知道我在这里做错了什么?我很难过。

最佳答案

试试这个

SInt16 *dataBuffer = THIS->dataBuffer;
double *dywapBuffer = THIS->dywapBuffer;
for (int i = 0; i < THIS.bufferCapacity; i++)
{
// compiler will do the pointer arithmetic correctly for you
double* doublePointer = dywapBuffer + i;
SInt16* intPointer = dataBuffer + i;
*doublePointer = (double)(*intPointer);
}
double frequency = dywapitch_computepitch(&THIS->pitchtracker, dywapBuffer, 0, frames);

你不需要像 dywapBuffer + (i * sizeof(double)) 这样的东西,因为它没有意义。编译器知道 dywapBuffer 的类型是 double *,所以 dywapBuffer + 1 会将地址增加 sizeof(double).

关于ios - Objective-C:单步执行 double* 数组时 EXC_BAD_ACCESS code=2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750391/

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