我正在研究一个处理队列的实验室,我认为这不是完全相关的。 我的任务是创建一个“优先级队列”,我可以想到的最佳方式如下
void IntQueue::enqueue(int num,int priorityOfEntry) { if (isFull()) cout << "The queue is full.\n"; else { // Calculate the new rear position //insert correct lab code here haha if (priorityOfEntry == 1) { rear = (rear + 1) % queueSize; queueArray[rear] = num; queueSize++; } else if (priorityOfEntry == 2) { queueSize++; int* newArray = new int[queueSize]; newArray[0] = num; for(int counter = 0;counter< queueSize; counter++) { newArray[counter+1] = queueArray[counter]; } queueArray = newArray; delete [] newArray; } else cout << "invalid priority" << endl; // Insert new item // Update item count numItems++; } }我只有2个优先级,1和2,我在主程序中解释。 当它们都具有相同的优先级时,它当然可以正常工作,但是当我优先考虑它时,它会在我的析构函数中抛出一个错误。
我真的不认为这是接近这个实验室的正确方法,但它似乎工作..至少如果我能真正得到这个内存错误修复。 我认为唯一的问题可能在于我更改了析构函数认为它将删除的地址..但是我认为指针已经对此有所解释。 我知道我需要学习调试我自己的程序。 我真的这样做。 但有时我只是盯着代码,那里只有一堵砖墙。 猜猜这是正确方向的推动。
I am working on a lab dealing with queues, which I don't think is entirely relevant. My task is to create a "priority queue" and the best way I could think of to do it is as follows
void IntQueue::enqueue(int num,int priorityOfEntry) { if (isFull()) cout << "The queue is full.\n"; else { // Calculate the new rear position //insert correct lab code here haha if (priorityOfEntry == 1) { rear = (rear + 1) % queueSize; queueArray[rear] = num; queueSize++; } else if (priorityOfEntry == 2) { queueSize++; int* newArray = new int[queueSize]; newArray[0] = num; for(int counter = 0;counter< queueSize; counter++) { newArray[counter+1] = queueArray[counter]; } queueArray = newArray; delete [] newArray; } else cout << "invalid priority" << endl; // Insert new item // Update item count numItems++; } }I only have 2 priority levels, 1 and 2, that I explain in the main program. when they all have equal priority it of course works fine, but when I bump on up in priority it throws an error at my destructor.
I really don't think this is the right way to approach this lab, but It seems to work.. at least if I can actually get this memory error fixed. I figure the only problem could be in that I change the address of what the destructor thinks it will delete.. but I thought pointers would already kind of account for that. I understand I need to learn to debug my own programs. I really do. but sometimes I just stare at code and there is nothing but a brick wall there. Guess that's what a nudge in the right direction is for.
最满意答案
在此之后, queueArray是一个悬挂指针 :
queueArray = newArray; // Both 'queueArray' and 'newArray' point to // the same memory after this assignment. delete [] newArray;作为queueArray指向的内存已被delete d。 任何访问或销毁queueArray尝试queueArray访问已经被销毁的内存。 正确的顺序是:
delete[] queueArray; queueArray = newArray;此外,执行复制的for循环中还有可能出现越界访问:
for(int counter = 0;counter< queueSize; counter++) { // When 'counter == queueSize - 1' // 'newArray[counter + 1]' is one past the end. newArray[counter+1] = queueArray[counter]; }queueArray is a dangling pointer after this:
queueArray = newArray; // Both 'queueArray' and 'newArray' point to // the same memory after this assignment. delete [] newArray;as the memory that queueArray is pointing to has been deleted. Any attempt to access or destroy queueArray is accessing memory that has already been destroyed. The correct order is:
delete[] queueArray; queueArray = newArray;Additionally, there is a potential out-of-bounds access in the for loop that performs the copying:
for(int counter = 0;counter< queueSize; counter++) { // When 'counter == queueSize - 1' // 'newArray[counter + 1]' is one past the end. newArray[counter+1] = queueArray[counter]; }更多推荐
发布评论