2013-05-17 2 views
0

순환 버퍼를 사용하여 주가를 저장하는 프로그램을 작성하고 있습니다. 버퍼가 갱신 될 때마다 특정 조건이 충족되는지 여부에 따라 버퍼가 확장 및 축소 될 수 있어야합니다. 그러나 버퍼를 한 번 확장 할 수있는 문제가 있습니다. 그런 다음 다시 확장하려고하면 segfaults가 발생합니다. 새 버퍼를 사용하고 이전 버퍼가 사용했던 메모리를 해제 한 후 이전 버퍼에 할당합니다. 내 코드는 다음과 같습니다.해제 된 메모리를 할당 할 때 segfaults

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"에 대해 두 번째 메모리 할당을 시도 할 때 segfaults가 발생합니다.

아이디어가 있으십니까?

+2

우리가 조금 적은 코드를 가질 수 ? 가능한 가장 작은 예제로 문제점을 최소화하고 질문을 업데이트하십시오. –

+0

은 버퍼 확장 문제 만 보여주기 위해 편집했습니다. – user2252004

+1

'cbuf_update '의 마지막 줄이 매우 의심 스럽습니다. '* cb_ptr = * newBuffer'는 이상한 일을 할 것입니다. 우선, 이전 줄에서'cb_ptr'을 해방하십시오. 그래서 그것에 무언가를 할당하는 것은 좋지 않습니다. '* (ptr) = * (ptr)'를 사용하면 객체의 비트 복사를 수행 할 수 있습니다. 이상적인 객체 일 수도 있고 그렇지 않을 수도 있습니다. 'cbuf_update'의 첫 번째 인자가'cbuf ** '가되기를 원합니다. 그래서 마지막 라인은'* cb_ptr = newBuffer'가 될 수 있습니다. 물론,'cb_ptr'에 대한 다른 모든 참조는'* cb_ptr->'처럼 보일 것입니다. 이 문제가 해결 될 것이라고 확신하지는 않지만 메모리 손상입니다. –

답변

1

이 줄 :

*cb_ptr = *newBuffer; 

분명히 뭔가 원하지 않는 않습니다. 당신은 아마 의미 :

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

그리고 주에 : 당신의 cb_ptr 매개 변수 in/out 경우

cb_ptr = newBuffer; 

, 다음과 같이 사용되어야한다

cbuf_update(&cb1, 60, 1.291) ; 
관련 문제