C Issues with pointers and a queue -
C Issues with pointers and a queue -
i using self-written queue library next structure:
#ifndef myqueue_ #define myqueue_ #ifndef set_queue_size #define set_queue_size 10 #endif typedef struct queue queue; /* ** creates , initializes queue , prepares usage ** homecoming pointer newly created queue */ queue* queuecreate(); /* ** add together element of generic type queue */ void enqueue(queue* queue, void* element); /* ** delete queue memory; set queue null ** queue can no longer used unless queuecreate called 1 time again */ void queuedestroy(queue** queue); /* ** homecoming number of elements in queue */ int queuesize(queue* queue); /* ** homecoming pointer top element in queue */ void* queuetop(queue* queue); /* ** remove top element queue */ void dequeue(queue* queue); #endif //myqueue_
now, i'm having issues putting in , receiving out circular queue. queue has been tested , shouldn't give issues.
running code below (it's extract of total program) 2 prints don't print same values. pointing wrong?
#include <stdlib.h> #include <stdio.h> #include <time.h> #include "libs/myqueue.h" struct package{ unsigned sensorid:12; unsigned sequence:4; unsigned flag:2; unsigned sign:1; unsigned value:12; unsigned parity:1; }; typedef struct bundle packs; struct sensor_time{ packs mypack; time_t time; }; typedef struct sensor_time sensor_time; queue* queue = null; int main(void){ queue = queuecreate(); if(queue == null){ printf("error creating circular buffer.\n"); exit(exit_failure); } sensor_time * mydata = malloc(sizeof(sensor_time)); mydata->mypack.sensorid = 1; mydata->mypack.value = 20; mydata->time = time(null); printf("enqueued: id: %d, value: %d, time: %lu\n", mydata->mypack.sensorid, mydata->mypack.value, mydata->time); enqueue(queue, (void *)mydata); sensor_time * mysens = (sensor_time *)queuetop(queue); printf("data read: id: %d, value: %d time: %lu", mysens->mypack.sensorid, mysens->mypack.value, mysens->time); homecoming 1; }
what gets printed:
for completeness, here queue implementation:
/*----- include files -----*/ #include <stdlib.h> #include <stdio.h> #include "myqueue.h" /*----- variables -----*/ typedef void * datatype; struct queue{ datatype elements[set_queue_size]; size_t count; int front; int rear; }; typedef struct queue queue; typedef queue *myqueue; /*----- queuecreate -----*/ queue* queuecreate(void){ #ifdef debug printf("queuecreate called.\n"); #endif queue* qu = malloc(sizeof(queue)); qu->count = 0; qu->front = 0; qu->rear = 0; homecoming qu; } /*----- queuecreate -----*/ void enqueue(queue* queue, datatype element){ #ifdef debug printf("enqueue called, on queue %p.", queue); int location = queue->rear; #endif if(queue->count == set_queue_size){ printf("queue full.\n"); }else{ queue->elements[queue->rear] = element; queue->count++; queue->rear++; if(queue->rear == set_queue_size - 1){ queue->rear = 0; } } #ifdef debug printf(" element added on location %d.\n", location); #endif } /*----- queuedestroy -----*/ void queuedestroy(queue** queue){ #ifdef debug printf("queuedestroy called on %p\n", queue); #endif free(*queue); *queue = null; } /*----- queuesize -----*/ int queuesize(queue* queue){ #ifdef debug if(queue->count > 0){ printf("queuesize called. size %d.\n", (int)queue->count); } #endif homecoming queue->count; } /*----- queuetop -----*/ void* queuetop(queue* queue){ #ifdef debug printf("queuetop called\n"); #endif if(queue->count == 0){ homecoming null; }else{ homecoming &(queue->elements[queue->front]); } homecoming null; } /*----- dequeue -----*/ void dequeue(queue* queue){ #ifdef debug printf("dequeue called on %p.", queue); int location = queue->front; #endif if(queue->count == 0){ printf("queue empty.\n"); }else{ queue->front++; if(queue->front == set_queue_size - 1){ queue->front = 0; } queue->count--; } #ifdef debug printf(" removed element on location %d.\n", location); #endif }
your queue stores elements of type void*
. however, queuetop
returns pointer first element, create of type void**
. fact function declared returning void*
instead of datatype*
makes confusing, it's legal because pointer can converted void*
, including double pointer.
when add together element queue, cast sensor_time *
void*
. later when calling queuetop
, take homecoming value , cast sensor_time *
, wrong, because pointer pointer.
to fix, you'd need either alter queuetop
homecoming element rather pointer element, or alter calling code dereference returned pointer.
c pointers queue
Comments
Post a Comment