I know that there’s something fishy about the malloc part, but I’m having trouble seeing what’s unsafe about this:
//que structure
typedef struct queue{
int *que; // the actual array of queue elements
int head; // the head index in que of the queue
int count; /// number of elements in queue
int size; // max number of elements in queue
} QUEUE;
void qManage(QUEUE **qptr, int flag, int size){
if(flag){
/* allocate a new queue */
*qptr = malloc(sizeof(QUEUE));
(*qptr)->head = (*qptr)->count = 0;
(*qptr)->que = malloc(size * sizeof(int));
(*qptr)->size = size;
}
else{
/* delete the current queue */
(void) free((*qptr)->que);
(void) free(*qptr);
}
}
I’m pretty sure the problem this:
What happens if you pass a negative value for 'size'?Another possible issue is that you do not check
*qptrforNULLafter you allocate, however, rarely would that be a problem in actual code, if it would ever happen, you have other errors to worry about.