Question

I am attempting to develop a dynamically-allocated circular-buffer in C using two structs. One holds detailed information and another is essentially used as a pointer from main to the circular-buffer structure (as there will be multiple arrays allocated at runtime).

Since it is a circular-buffer, I have a pointer "next" which points to the next item in the array (so last array index points to the first, etc.)

These are the two struct objects I have:

typedef struct {
    int a;
    int b;
    struct1 *next;   // pointer to next struct1 object in array
} struct1;

typedef struct {
    struct1 *curr;     
    struct1 *start = NULL;
    struct1 *end = NULL;
} struct2;

I then have my initialize function that is called from main to initiate a new circular-buffer.

This is the part where I am not entirely sure what to do.

#define minSize 10
struct2 * initialize()
{   
    struct2 **newBuf = malloc(sizeof(*newBuf));
    newBuf->malloc(sizeof(*newBuf->quotes) * newBuf->minSize);

    // set the start pointer
    newBuf.curr[0] = newBuf->start;
    newBuf.curr[0]->next = NULL;

    for (int i = 1; i < minSize; i++)
    {
        struct1 *new = NULL;     
        newBuf.curr[i] = new;    // make index i = NULL
        // have the previous index point to the "next" current
        if (i > 0)
            newBuf.curr[i-1]->next = newBuf.curr[i];
    }

    // connect last index with first
    newBuf.curr[minSize - 1]->next = newBuf.curr[0];

    // set the end pointer  
    newBuf->end = newBuf->start;

    return newBuf;
}

From searching I found this answer on how to initialize an array of structs within a struct by using malloc for initially allocating the space, but am confused how my code would line up since I have pointers to define start and end of the circular-buffer defined in struct2, as well as the next pointer as part of struct1.

Additionally, I've chosen to define ***newBuf* instead of **newBuf* as I was considering it as a pointer to pointers in a way (thinking about singly-linked lists). Though, please correct me if I am wrong.

I've done dynamically allocated circular-buffers in Java, but not C nor C++, so I am having a hard time figuring out the differences in how to initialize everything. I'm basically stuck at this mess and not sure where to go next.

Any help that can be given would be much appreciated!

Was it helpful?

Solution

The reason you're running into trouble is because you're trying to have the pointer to a pointer, rather than just using an ordinary pointer. You want to access the pointer that is contained at the address pointed to by the first pointer. As it stands you're trying to access a member that is outside of the memory space of the original pointer's address (which is only as large as an address). And then you're running into trouble because you aren't initializing your array 'curr' either. Another thing I did that doesn't really matter but helps you understand pointers is made your array a pointer- which is how arrays work in C. The array is simply the address of the first member of the array, and when you index into the array, it just adds an offset to that address = index * sizeof(yourstruct).

What you want is

typedef struct {
   struct1 *curr;     
   struct1 *start = NULL;
   struct1 *end = NULL;
} struct2;

#define minSize 10
struct2* initialize()
{   
 struct2 *newBuf = (struct2 *) malloc(sizeof(struct2));
 newBuf->curr = (struct1 *) malloc(sizeof(struct1) * minSize);

// set the start pointer
 newBuf.curr[0] = newBuf->start;
 newBuf.curr[0]->next = NULL;

 for (int i = 1; i < minSize; i++)
 {
    struct1 *new = (struct1 *) malloc(sizeof(struct1));
    newBuf.curr[i] = new;
    newBuf.curr[i-1]->next = newBuf.curr[i];
 }
  // connect last index with first
  newBuf.curr[minSize - 1]->next = newBuf.curr[0];
  // set the end pointer  
  newBuf->end = newBuf->start;
  return newBuf;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top