使用C实现纯外部数据时内存损坏
我目前正在尝试使用C实现纯数据的外部接口。我已经有一段时间没有使用C了,而且我有一个内存损坏问题。我不知道该怎么办,所以我请求你的帮助 这是代码。请记住,这是一个纯数据的C代码使用C实现纯外部数据时内存损坏,c,puredata,C,Puredata,我目前正在尝试使用C实现纯数据的外部接口。我已经有一段时间没有使用C了,而且我有一个内存损坏问题。我不知道该怎么办,所以我请求你的帮助 这是代码。请记住,这是一个纯数据的C代码 t_int *myfft_tilde_perform(t_int *w) { t_myfft_tilde *fft = (t_myfft_tilde *)w[1]; t_float *in = (t_float *)w[2]; t_float *out = (t_float
t_int *myfft_tilde_perform(t_int *w) {
t_myfft_tilde *fft = (t_myfft_tilde *)w[1];
t_float *in = (t_float *)w[2];
t_float *out = (t_float *)w[3];
int n = (int)w[4];
int i;
for(i=0; i<n; i++)
{
fft->bigbuffer[fft->nextindex] = in[i];
fft->nextindex = (fft->nextindex+1)%TAILLE;
}
if (!fft->isfull && fft->nextindex==0)
{
fft->isfull=1;
}
if (fft->isfull)
{
for(i=0; i<TAILLE; i++)
{
fft->bigbuffercpy[i] = fft->window[i] * fft->bigbuffer[i];
}
rdft(TAILLE, 1, fft->bigbuffercpy, fft->bitshuffle, fft->weighting);
if (fft->nextindex==0)
{
i=(TAILLE-1)-64;
}
else
{
i=fft->nextindex-64;
}
int j;
for(j=0; j<64; j++)
{
out[j] = fft->bigbuffercpy[i+j];
}
}
return (w+5);
}
void myfft_tilde_dsp(t_myfft_tilde *x, t_signal **sp) {
dsp_add(myfft_tilde_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}
void myfft_tilde_free(t_myfft_tilde *x) {
if (x->bitshuffle)
freebytes(x->bitshuffle, TAILLE*2*sizeof(int));
if (x->weighting)
freebytes(x->weighting, TAILLE*2*sizeof(float));
if (x->window)
freebytes(x->window, TAILLE*sizeof(float));
if (x->bigbuffer)
freebytes(x->bigbuffer, TAILLE*sizeof(float));
if (x->bigbuffercpy)
freebytes(x->bigbuffercpy, TAILLE*sizeof(float));
}
void *myfft_tilde_new(void) {
t_myfft_tilde *fft = (t_myfft_tilde *)pd_new(myfft_tilde_class);
fft->x_out = outlet_new(&fft->x_obj, &s_signal);
fft->bitshuffle = (int *)calloc(TAILLE*2, sizeof(int));
fft->weighting = (float *)calloc(TAILLE*2, sizeof(float));
fft->window = (float *)calloc(TAILLE, sizeof(float));
fft->bigbuffer = (float *)calloc(TAILLE, sizeof(float));
fft->bigbuffercpy = (float *)calloc(TAILLE, sizeof(float));
int i;
for(i=0; i<TAILLE; i++)
{
fft->window[i] = 0.54 - 0.46*cos(TWOPI*i/TAILLE);
}
fft->nextindex=0;
fft->isfull=0;
init_rdft(TAILLE*2, fft->bitshuffle, fft->weighting);
return (void *)fft;
}
void myfft_tilde_setup(void) {
myfft_tilde_class = class_new(gensym("myfft~"),
(t_newmethod)myfft_tilde_new,
(t_method)myfft_tilde_free,
0, sizeof(t_myfft_tilde),
CLASS_DEFAULT, A_GIMME, 0);
CLASS_MAINSIGNALIN(myfft_tilde_class, t_myfft_tilde, f);
class_addmethod(myfft_tilde_class, (t_method)myfft_tilde_dsp,
gensym("dsp"), 0);
}
我甚至试图删除我所做的所有calloc,但错误仍然发生 您错误地使用了
class\u new()
它的论点是:
- 名字
- 建造师
- 析构函数
- 成员数据结构的大小
- 旗帜
- 构造函数的arg规范
- a
(终止符)0
大小
和标志
,给出了0
字节的有效大小pd_new()
然后将为fft
-struct分配0
(零,零)字节,一旦访问struct成员,就会导致内存损坏(下一行就是这样做的)
除此之外:对于样本值,请始终使用t\u sample
而不是t\u float
(或float
!!)。
始终使用
t\u float
而不是float
作为消息编号。请输入malloc()
/calloc()
的返回值。什么是TAILLE
?并且:它是否大于64?我无法理解这段代码的作用,但我建议您开始调试。您可以逐步查看代码以查看错误抛出的位置,也可以“缩小”代码规模,从空扩展开始,逐渐添加内容(并逐渐增加内存使用),直到错误再次发生。然后您就知道错误在哪里了。强制转换已删除。但错误仍然存在:TAILLE是大缓冲区的大小。其当前值为1024。Puredata只处理64字节的缓冲区,它太小,无法处理声音(这是本程序的目的),因此我必须使用更大的缓冲区,以使数据在计算过程中更相关。
*** Error in `puredata': malloc(): memory corruption: 0x084f4570 ***
Pd: signal 6