gpt4 book ai didi

尝试释放不再需要使用的内存时出现 C 段错误

转载 作者:行者123 更新时间:2023-12-04 04:56:42 27 4
gpt4 key购买 nike

我正在用 C 编写一个循环缓冲区程序。缓冲区必须能够根据每次使用新报价更新缓冲区时检查的某些条件进行扩展和收缩(缓冲区包含股票报价)。但是,当我尝试扩展缓冲区并释放旧的(较小的)缓冲区时,我从 gilbc 收到“无效的下一个大小”错误。

我的扩展功能如下:

cbuf* expandBuffer(cbuf *cb_ptr){


int newSize;
newSize = (cb_ptr->maxSize * 2) -1;
cbuf *tempBuffer = malloc(sizeof(cbuf) + newSize * sizeof(quote));
tempBuffer ->maxSize = cb_ptr->maxSize * 2;
tempBuffer ->start = cb_ptr->start;
tempBuffer ->end = cb_ptr->end;
tempBuffer ->freeSlots = tempBuffer->maxSize - cb_ptr->maxSize;

int x;
int counter;
counter = 0;

for(x = cb_ptr->end; x < cb_ptr->maxSize; x ++){

tempBuffer->quoteBuffer[counter].time = cb_ptr->quoteBuffer[x].time;
tempBuffer->quoteBuffer[counter].rate = cb_ptr->quoteBuffer[x].rate;
counter ++;
}
int y;
for(y = 0; y < cb_ptr->start; y ++){

tempBuffer->quoteBuffer[counter].time = cb_ptr->quoteBuffer[y].time;
tempBuffer->quoteBuffer[counter].rate = cb_ptr->quoteBuffer[y].rate;
counter++;
}

tempBuffer->start = cb_ptr->maxSize;
tempBuffer->end = 0;
free(cb_ptr);
return tempBuffer;

}

我的更新功能如下:
void cbuf_update(cbuf *cb_ptr, unsigned int time, double rate){

*Code*

if(cb_ptr->freeSlots == 0){
printf("\n\nEXPANDING CIRCULAR BUFFER!\n\n");
*cb_ptr = *(expandBuffer(cb_ptr));


}

*More code here edited out since it doesnt pertain to question.
}

最后我的主要内容:
int main(){

cbuf *cb1 ;
cb1 = cbuf_init() ;
cbuf_update(cb1, 60, 1.291) ;
cbuf_update(cb1, 63, 1.287) ;
cbuf_update(cb1, 63, 1.231) ;
cbuf_update(cb1, 69, 1.229) ;
cbuf_update(cb1, 72, 1.247) ;
cbuf_update(cb1,361,1.291);
cbuf_update(cb1, 411, 1.291) ;
cbuf_update(cb1, 412, 1.281) ;
cbuf_update(cb1, 413, 1.292) ;
cbuf_update(cb1, 414, 1.284) ;
cbuf_update(cb1, 414, 1.290) ;
cbuf_update(cb1, 511, 1.241) ;
cbuf_update(cb1, 512, 1.251) ;
cbuf_update(cb1, 513, 1.232) ;
cbuf_update(cb1, 514, 1.202) ;
cbuf_update(cb1, 517, 1.119) ;
cbuf_update(cb1, 551, 1.080) ;
cbuf_update(cb1, 552, 1.081) ;
cbuf_update(cb1, 553, 1.079) ;
cbuf_update(cb1, 554, 1.088) ;
cbuf_update(cb1, 561, 1.072) ;
cbuf_update(cb1, 562, 1.113) ;
cbuf_update(cb1, 563, 1.091) ;
cbuf_update(cb1, 564, 1.092) ;
cbuf_update(cb1, 571, 1.089) ;
cbuf_update(cb1, 572, 1.073) ;
cbuf_update(cb1, 573, 1.061) ;
cbuf_update(cb1, 574, 1.111) ;
cbuf_update(cb1, 581, 1.119) ;
cbuf_update(cb1, 582, 1.123) ;
cbuf_update(cb1, 583, 1.151) ;
return 0;
}

该行出现段错误:
cbuf *tempBuffer = malloc(sizeof(cbuf) + newSize * sizeof(quote));

在 expandBuffer 中,它已经成功扩展一次。如果我需要多次扩展缓冲区,我会在该行上遇到段错误。

但是,如果我注释掉:
    free(cb_ptr);

我不再遇到段错误。然而,cb_ptr(“旧”缓冲区被新的、更大的缓冲区取代)必须被释放,因为它不再被使用。我很困惑为什么释放它并将一个新的更大的缓冲区返回到 cbuf_update 中的 cb_ptr 会导致段错误。

最佳答案

问题是这样的:

*cb_ptr = *(expandBuffer(cb_ptr));

expandBuffer您释放了指针,但随后您在赋值中取消引用了现在已释放的指针。这会导致未定义的行为,这可能(在您的情况下肯定)导致崩溃。

我建议您改为通过引用将指针(即指向指针的指针)传递给 cbuf_update功能:
void cbuf_update(cbuf **cb_ptr, unsigned int time, double rate)
{
/* ... */

if((*cb_ptr)->freeSlots == 0)
{
printf("\n\nEXPANDING CIRCULAR BUFFER!\n\n");
*cb_ptr = expandBuffer(*cb_ptr);
}

/* ... */
}

使用指针的地址运算符调用新函数:
cbuf_update(&cb1, 60, 1.291) ;

关于尝试释放不再需要使用的内存时出现 C 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16664789/

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