Solving: Why Destroying a Queue Doesn't Work

  • Thread starter Thread starter AcecA
  • Start date Start date
  • Tags Tags
    Queue
Click For Summary
SUMMARY

The discussion addresses the issue of a C/C++ algorithm failing to properly destroy a queue data structure. The provided code attempts to delete the queue elements and the queue itself but does not effectively nullify the pointer reference in the calling context. The main problem lies in the misunderstanding of how to traverse and delete each element in a linked queue structure, as the current implementation only deletes the first element and not the entire queue. Proper queue destruction requires iterating through each element to ensure all are deleted before nullifying the pointer.

PREREQUISITES
  • Understanding of C and C++ memory management
  • Knowledge of data structures, specifically queues
  • Familiarity with pointers and dynamic memory allocation
  • Experience with debugging and analyzing code behavior
NEXT STEPS
  • Research "C++ linked list implementation" to understand proper element traversal and deletion
  • Learn about "C++ smart pointers" to manage memory more effectively
  • Study "C dynamic memory allocation" to grasp the implications of memory leaks
  • Explore "C++ destructor functions" for proper resource management in classes
USEFUL FOR

Software developers, computer science students, and anyone involved in implementing or debugging queue data structures in C or C++.

AcecA
Messages
12
Reaction score
0

Homework Statement


All I need is an explanation as to why the algorithm I've been given doesn't destroy the queue properly. When I use the code below, and I check if q is NULL or not, it says it isn't, and I'd like to know why. Thanks :)

Homework Equations


None, this is computer science.
Langauge is C with some C++ conventions, by the way

The Attempt at a Solution



Code:
typedef int Element;

struct _Queue {
     Element *elts;
     int head;
     int capacity;
     int count;
};

typedef _Queue* Queue;

void DestroyQueue(Queue q)
{
     delete[] q->elts;
     q->elts = NULL;
     q->head = 0;
     q->count = 0;
     delete q;
     q = NULL;
}
 
Physics news on Phys.org
AcecA said:

Homework Statement


All I need is an explanation as to why the algorithm I've been given doesn't destroy the queue properly. When I use the code below, and I check if q is NULL or not, it says it isn't, and I'd like to know why. Thanks :)


Homework Equations


None, this is computer science.
Langauge is C with some C++ conventions, by the way

The Attempt at a Solution



Code:
typedef int Element;

struct _Queue {
     Element *elts;
     int head;
     int capacity;
     int count;
};

typedef _Queue* Queue;

void DestroyQueue(Queue q)
{
     delete[] q->elts;
     q->elts = NULL;
     q->head = 0;
     q->count = 0;
     delete q;
     q = NULL;
}

I see a couple of problems. Your elts member is a pointer to Element, which means it is a pointer to an int. It probably should be a pointer to a _Queue, otherwise I can't understand what is is being used for.

Normally in a queue data type, you have one member that points to another queue element, that in turn points to another queue element, and so on until the last element in the queue. The pointer in the last queue item has the value NULL, marking the end of the queue.

You can't be sitting in one element and delete all the other elements in one fell swoop. You need to traverse the queue to the next-to-last element, delete the last element, back up to the previous element, delete the one that that element is pointing to, and so on, backing you way until you get to the first item in the queue. In case you didn't follow that, you need to back your way through the queue, deleting the current last item.
 

Similar threads

Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
10K
  • · Replies 3 ·
Replies
3
Views
6K