I implemented the basic queue structure in C using void pointers. The procedure is as follows:
- structure initialization - I set the size of the type of variable that should be stored in the queue
- push - I pass a pointer to a variable to be saved, the queue then grabs a copy for myself
- front - the structure returns void * to the front element. I can just grab a pointer or
memcpy() to have a local copy.
The structure itself is as follows:
struct queue { void* start; //pointer to the beginning of queue void* end; //-||- to the end size_t memsize; //size of allocated memory, in bytes size_t varsize; //size of a single variable, in bytes void* initial_pointer; //position of the start pointer before pop() operations };
the start and end are just void pointers pointing to a location within the currently allocated memory block. If I click the items in the queue, I increase the final pointer to varsize . If I pop (), I simply reduce the final pointer to varsize as well.
I donβt think I should post the function code here, it's over 100 lines.
Question: Is it considered good or bad practice? Why not)?
Note. I know that there are many other options for queuing in C. I'm just asking about the quality of this.
EDIT: Implementation is available here: http://89.70.149.19/stuff/queue.txt (remove spaces)
source share