0

This is the code for deleting the front item from the queue I have already made. I try to understand that itemPtr will be used somehow in main.. not known yet but that's not the point. I wonder whether

if (queue->count == 0)

can be used instead of

if (!queue->count)

Are the NULL and 0 used in the same way?

To add, what does this do?

free(deleteLoc)

I thought deleteLoc will disappear after this dequeue method ends but why add this? This is not my code so I'm curious. Does it obliterate the memory that deleteLoc contained or queue->front ? Isn't the point is to make the data itself be disappeared?

Thank you.

bool dequeue(H* queue ,void* itemPtr)
{
    node* deletecLoc;
    if (!queue->count)
        return false;
    *itemPtr = queue->front->dataPtr;
    deleteLoc = queue->front;
    if (queue->count == 1)
        queue->front = queue->rear = NULL;
    else
        queue->front = queue->front->next;
    (queue->count)--;
    free(deleteLoc);
    return true;
}
Alicia May
  • 43
  • 5
  • First of all, `NULL` is the symbolic name of a null *pointer*. Secondly, in C everything non-zero is considered true, while zero is false. That explains what the logical negation operator `!` does, if `queue->count` is zero (and therefore false) then `!queue->count` will be true. All of this should have been known if you [read a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). Please start there. – Some programmer dude Nov 04 '17 at 12:27
  • And please keep it to one question per question. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 04 '17 at 12:28

2 Answers2

2

I wonder whether

if (queue->count == 0)

can be used instead of

if (!queue->count)

Yes, you can always express an implicit comparison to zero with an explicit comparison and vice versa.

Are the NULL and 0 used in the same way?

In the context of implicit comparison to zero performed by if, while, and other control statements, comparison to NULL and comparison to zero behave in the same way.

what does this do?

free(deleteLoc)

To understand this you need to look through the code of enqueue and find a call to malloc or calloc. Essentially, free is an "undo" for malloc, telling the system that you are no longer using the memory allocated to it dynamically.

Does it obliterate the memory that deleteLoc contained or queue->front

It may or may not change the content of the memory pointed to by deleteLoc, but it does not matter, because that memory becomes off-limits to your program: accessing it after a call to free is undefined behavior.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

if (queue->count == 0) will do the exact same thing as if (!queue->count) because in C if something evaluates to 0, if you treat it as a boolean it will evaluate to false.

In this context, yes, NULL and 0 will achieve the same thing. NULL refers to a null pointer, whereas 0 is usually the number zero, but they evaluate to the same thing.

Finally, free will get rid of memory allocated earlier. You've not shown all of the code but somewhere in the insert function, there will be a malloc (or similar function) which allocates memory for you to store your node in. Free gets rid of that memory and you need to call it when you are done with a node.

Steve
  • 1,747
  • 10
  • 18
  • Actually in C it's common for `NULL` to be of type `void *`. Which is (semantically) very different from the `int` value `0`. The two should not really be mixed. – Some programmer dude Nov 04 '17 at 12:32