Note that there are some explanatory texts on larger screens.

plurals
  1. POend pointer problem of a circular buffer
    text
    copied!<p>I'm using Visual c++.</p> <p>I'm trying to implement a circular buffer, this CB must handle a specific type of data...in fact, it's a structure data where we have some kind of raw data to be stored in a char type and a date associated to that data...this has been implemented using a strucuture.</p> <p>here is the code for more details:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;time.h&gt; #include &lt;windows.h&gt; //data=date_label+raw_data typedef struct DataFragment { char data[4]; clock_t date; }DataFragment; typedef struct CircularBuffer { DataFragment *buffer; // data buffer DataFragment *buffer_end; // end of data buffer size_t capacity; // maximum number of items in the buffer size_t count; // number of items in the buffer size_t sz; // size of each item in the buffer DataFragment *head; // pointer to head DataFragment *tail; // pointer to tail } CircularBuffer; void cb_init(struct CircularBuffer *cb, size_t capacity, size_t sz) { if((cb-&gt;buffer = (DataFragment*) malloc(capacity * sz))!=NULL) puts("success alocation"); //if(cb-&gt;buffer == NULL) //handle error cb-&gt;buffer_end = (DataFragment *)cb-&gt;buffer + (capacity-1)*sz; cb-&gt;capacity = capacity; cb-&gt;count = 0; cb-&gt;sz = sz; cb-&gt;head = cb-&gt;buffer; cb-&gt;tail = cb-&gt;buffer; } void cb_free(struct CircularBuffer *cb) { free(cb-&gt;buffer); // clear out other fields too, just to be safe } void cb_push_back(struct CircularBuffer *cb, const DataFragment *item) { //if(cb-&gt;count == cb-&gt;capacity) //handle error when it's full memcpy(cb-&gt;head-&gt;data, item-&gt;data,4); cb-&gt;head-&gt;date=item-&gt;date; cb-&gt;head = (DataFragment*)cb-&gt;head + cb-&gt;sz; if(cb-&gt;head == cb-&gt;buffer_end) cb-&gt;head = cb-&gt;buffer; cb-&gt;count++; } void cb_pop_front(struct CircularBuffer *cb, DataFragment *item) { //if(cb-&gt;count == 0) //handle error memcpy(item-&gt;data, cb-&gt;tail-&gt;data,4); item-&gt;date=cb-&gt;tail-&gt;date; cb-&gt;tail = (DataFragment*)cb-&gt;tail + cb-&gt;sz; if(cb-&gt;tail == cb-&gt;buffer_end) cb-&gt;tail = cb-&gt;buffer; cb-&gt;count--; } int main(int argc, char *argv[]) { struct CircularBuffer pbuf; pbuf.buffer=NULL; pbuf.buffer_end=NULL; pbuf.capacity=0; pbuf.count=0; pbuf.head=NULL; pbuf.sz=0; pbuf.tail=NULL; struct CircularBuffer *buf= &amp;pbuf; size_t sizz = sizeof(DataFragment); //initialisation of the circlar buffer to a total bytes //of capacity*sizz=100*sizeof(struct DataFragment) cb_init(buf,100,sizz); //temporary container of data DataFragment temp,temp2; for(int i=0;i&lt;4;i++) temp.data[i]='k'; for(int i=0;i&lt;4;i++) temp2.data[i]='o'; //pushing temporary buffer to the CB...40*2=80&lt;capacity of the CB for(int i=0;i&lt;40;i++) { Sleep(20); temp.date=clock(); cb_push_back(buf,&amp;temp); Sleep(10); temp2.date=clock(); cb_push_back(buf,&amp;temp2); } DataFragment temp3; for(int i=0;i&lt;20;i++) { cb_pop_front(buf,&amp;temp3); printf("%d\n", temp3.data); //print integers....no need of end caracter } cb_free(buf); return 0; } </code></pre> <p>When I compile the code, everything is fine, but when I debug, I noticed a problem with the buffer_end pointer, it says bad_pointer....this happens if the capacity is greater than 56...I don't know why the pointer can't point to end of the buffer.But if the capacity is less than 56 the pointer points exactly on the end of the buffer</p> <p>If anyone knows why this happens like this, and how to fix it, please help me..</p> <p>thanks in advance </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload