0

I am working on an a queue and keep running into problems with enqueuing. Here is what I believe to be the relevant code:

typedef struct Qnode QNODE;
struct Qnode
{
  int length;
  QNODE* next;
  QNODE* prev;
};

typedef struct lqueue lQUEUE;
struct lqueue
{
   QNODE *head;
   QNODE *tail;
};

lQueue lqueue_init_default(void)
{
lQUEUE* pQ = NULL;
pQ = (lQUEUE*)malloc(sizeof(lQUEUE));
if (pQ != NULL)
{
    pQ->head = NULL;
    pQ->tail = NULL;
}
pQ->head = pQ->tail;
return pQ;
}

Status lqueue_henqueue(lQueue* hLQ, int lc)
{
lQUEUE* pLQ = (lQUEUE*)hLQ;
QNODE* new = (QNODE*)malloc(sizeof(QNODE));
if (new == NULL)
{
    printf("Couldn't allocate space.\n");
    return FAILURE;
}
new->length = lc;
new->next = pLQ->tail->next;

pLQ->tail = new;
return SUCCESS;
}

Whenever I try to run the program, I get this error during run time:
Exception thrown: read access violation. pLQ->tail was nullptr.
Why is it a null pointer? Does it have to do with the Initialization function?
Here is how it is called:

int cl = 0;//Individual car length
lQueue hLQ = lqueue_init_default();//Handle to the left queue
printf("Enter the length of the lcar:\n");
            scanf("%d", &cl);
            lqueue_henqueue(hLQ, cl);
Nairbnb
  • 1
  • 2

1 Answers1

0

Your code is highly prone to undefined behavior... Look at this if statement:

if (pQ != NULL)
{
    pQ->head = NULL; // This pointer is now 'NULL'
    pQ->tail = NULL; // This is also 'NULL'
}

Which should be this...

if (pQ != NULL)
{
    pQ->head = (QNODE*)calloc(1, sizeof(lQUEUE)); // This is proper pointer initialization...
    pQ->tail = (QNODE*)calloc(1, sizeof(lQUEUE));
}

And this:

lQueue lqueue_init_default(void)

should be this:

lQueue * lqueue_init_default(void) // Since you are returning a pointer...

You will see that the code works fine because there is no undefined behavior...

Note that you can never access an object that is assigned to NULL... (Only if you don't want your program to behave undefined...) So, this:

pQ->tail = NULL;

is not safe in the very least... Structural pointers being assigned to NULL are usually only seen when being destroyed... An example is given below...


Also, unrelated, but have a destructor for the structure and call it when you don't need the structure anymore, or it will leak the memory afterwards...

void destroy_lqueue(struct lqueue ** queue)
{
    if (queue != NULL)
        queue = NULL;
    free(queue);
}
Community
  • 1
  • 1
Ruks
  • 3,886
  • 1
  • 10
  • 22