I would like to pass a struct to a function by reference, create a new instance of a struct, destroy the original and return a new struct correctly. A specific example of such case is a queue resize function:
A queue structure itself:
// Queue for storage of pairs
typedef struct {
int n; // size of queue
int m; // no. of pairs in queue
int f; // index of front element
int r; // index of rear element
pair *pairs; // array of pairs
} queue;
Queue initialisation routine:
// Initialises queue
int init(int const *n, queue *q) {
q->n = *n;
q->m = 0;
q->f = 0;
q->r = 0;
q->pairs = (pair*) calloc(q->n, sizeof(pair));
return 0;
}
Queue destruction routine:
// Destroys queue
int destroy(queue *q) {
q->n = 0;
q->m = 0;
q->f = 0;
q->r = 0;
free(q->pairs);
return 0;
}
Enqueue routine:
// Adds pair to queue
int enqueue(pair *p, queue *q) {
// resize queue if necessary
if (isfull(q)) int status = resize(q);
if (q->m > 0) q->r = (++q->r) % q->n;
q->pairs[q->r] = *p;
q->m++;
return 0;
}
My take at the queue resizing routine (currently it crashes with a floating point exception). I believe to resize a queue correctly I need to pass a pointer to a queue pointer, but so far I am unable to achieve this.
// Resizes queue
int resize(queue *q) {
// initialise new queue
const int N = 2*q->n;
queue p;
init(&N, &p);
// copy pairs from old to new queue
for (int i = 0; i < q->m; i++) {
pair f = dequeue(q);
enqueue(&f, &p);
}
// destroy old queue
destroy(q);
// re-assign pointer to new queue
q = &p;
return 0;
}
enqueuebut passing the address of a local variable is a big no-no if you store the address in your structure: you'll find out that all variables have the same address, and which is invalid when f goes out of scopeenqueuecopies from provided pointer - this is how it is usually done.n.