0

我正在开发一个使用循环缓冲区来存储股票报价的程序。缓冲区必须能够根据缓冲区的每次更新期间是否满足某些条件来扩展和收缩。但是,我遇到了一个问题,我可以扩展一次缓冲区,但之后如果我尝试再次扩展它,我会遇到段错误。我正在使用一个新缓冲区,并在释放旧缓冲区正在使用的内存后将其分配给旧缓冲区。我的代码如下。

cbuf* cbuf_init(){

    cbuf *buffer = malloc(sizeof(cbuf) + 9 * sizeof(quote));
    buffer->currSize = 0;
    buffer->maxSize = startSize;
    buffer->start = 0;
    buffer->end = 0;
    buffer->freeSlots = startSize;
    return buffer;
}


void cbuf_update(cbuf *cb_ptr, unsigned int time, double rate){
    int threeFourths;    
    threeFourths = (3 * cb_ptr->maxSize)/4;


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

        int newSize;
        newSize = (cb_ptr->maxSize * 2) - 1;
        printf("Newsize: %d\n", newSize);
        cbuf *newBuffer = malloc(sizeof(cbuf) + newSize * sizeof(quote));
        printf("pastthe malloc\n");
        newBuffer->maxSize = cb_ptr->maxSize * 2;
        newBuffer->start = cb_ptr->start;
        newBuffer->end = cb_ptr->end;
        newBuffer->freeSlots = newBuffer->maxSize - cb_ptr->maxSize;

        int x;
        int counter;
        counter = 0;

        for(x = cb_ptr->end; x < cb_ptr->maxSize; x ++){
            newBuffer->quoteBuffer[counter].time = cb_ptr->quoteBuffer[x].time;
            newBuffer->quoteBuffer[counter].rate = cb_ptr->quoteBuffer[x].rate;
            counter ++;
        }
        int y;
        for(y = 0; y < cb_ptr->start; y ++){
            newBuffer->quoteBuffer[counter].time = cb_ptr->quoteBuffer[y].time;
            newBuffer->quoteBuffer[counter].rate = cb_ptr->quoteBuffer[y].rate;
            counter++;
        }
        newBuffer->start = cb_ptr->maxSize;
        newBuffer->end = 0;
        printf("newBuffer start: %d\n", newBuffer->start);
        free(cb_ptr);
        *cb_ptr = *newBuffer;


    }   



}

我的主要是:

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) ;
    cbuf_update(cb1, 584, 1.153) ;  
    cbuf_dump(cb1);
    return 0;

}

cbuf_dump 只是打印出与缓冲区有关的信息。使用此代码时,我得到以下输出:

EXPANDING CIRCULAR BUFFER!

Newsize: 19
pastthe malloc
newBuffer start: 10


EXPANDING CIRCULAR BUFFER!

Newsize: 39
Segmentation fault (core dumped)

如您所见,我插入了打印语句,以便在运行时我可以看到代码在出现段错误之前的位置。它第一次能够扩展,但是当它第二次尝试为新的“newBuffer”分配内存时,它会出现段错误。

有任何想法吗?

4

1 回答 1

1

这一行:

*cb_ptr = *newBuffer;

显然做了一些你不想要的事情。你可能的意思是:

cb_ptr = newBuffer;

如果您的cb_ptr参数是in/out,那么它应该用作:

void cbuf_update(cbuf **cb_ptr, unsigned int time, double rate)
...
if((*cb_ptr)->freeSlots == 0){
...
    *cb_ptr = newBuffer;
...

主要是:

cbuf_update(&cb1, 60, 1.291) ;
于 2013-05-17T19:32:07.483 回答