Hi all, I'm trying to implement a ring buffer with the following struct
/*head, tail are indexes of the head and tail of ring buffer
*count is the number of elements; size is the max size of buffer
*rbArray is an array to pointer of char used to store strings
*/
struct rb{
int head;
int tail;
int count;
int size;
char *rbArray[];
};
Then I use the following function to create a string buffer:
struct rb *create(int n){
/*allocate memory for struct*/
struct rb *newRb = (struct rb*)malloc(sizeof(struct rb)+ n*sizeof(char *));
assert(newRb);
int i;
for(i=0;i<n;i++)
newRb->rbArray[i] = NULL;
/*put head and tail at the beginning of array
initialize count, set max number of elements*/
newRb->head = 0;
newRb->tail = 0;
newRb->count = 0;
newRb->size = n;
return newRb;
}
I call this function in main:
struct rb *newRB = (struct rb*)create(100);
However, I have problem right at the step allocating memory for struct. In the debugging mode, I can see the value of head, tail, count, were assigned very strange large numbers but not 0. And program hangs after this very first step without throwing me any exception.
Could someone help me explain this problem please? How can I fix it?
Many thanks,