简体   繁体   中英

How to implement enque function without using the inbuilt malloc function?

The enque function in queue (using linked data structure) is generally used with malloc() function. However , I am trying to implement it little differrntly by avoiding the use of malloc() as follows.

I am printing the value of what holds in the rear twice. It prints the correct value for the first time, however, it gives a garbage value the second time.

 void enque(queue* qp, int x) // queue is a struct that holds front and rear node address
{
  queueNode a;  // queueNode is a struct with data and next pointer
  a.data = x;
  a.next = NULL;

  if(isEmpty(qp))
  {
    qp->front = &a;
    qp->rear = &a;
  }
  else
  {
    qp->rear->next = &a;
    qp->rear = qp->rear->next;
  }
}

The main function

int main()
{
 queue q;
 int c;
 initialize(&q);

 enque(&q, 11);

 printf("\n %d",(&q)->front->data);
 printf("\n %d",(&q)->front->data);

 return 0;
}

The output is as follows :

11

some garbage value

Why does it print the garbage value the second time and not 11?

Your program has the UB. This method will not work. If you do not want to use malloc you need to have a global pool of nodes.

a is a local variable, so the variable a is destroyed when the function enque finishes.

Now, in C and C++, "destroyed" doesn't necessarily mean that the data isn't at that address any more. It does mean that the data might not be at that address any more because the address can be reused for other purposes at any time. You can't expect it to still be there.

In practice, if you aren't using optimization , what is most likely to happen is that the space where enque 's local variables were stored will get reused by the next function call. There is no function call in between enque returning and reading (&q)->front->data ( printf isn't called until after the value is accessed), so the value of 11 is still at that location. Then printf has called, and printf has some local variables that get stored in the same memory location where enque 's local variables used to be. So the second time you read (&q)->front->data you are reading one of the local variables from the last printf call, instead of one of the local variables from enque .

Note: This is not reliable at all - you might get different results if you use a different compiler, a different version of the same compiler, a different version of your operating system, a different version of a library, if you turn on optimization, or if the planets aren't aligned properly. It could conceivably give you 11 both times, garbage values both times, crash, or even stranger things (i. That's why this is called undefined behaviour - literally meaning "the computer might do anything" - and you should avoid it. (You might think that the worst thing that could happen is that you get a garbage value, but when optimizers get involved they tend to be confused by stuff like this)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM