找不到未初始化的值(瓦尔格林德)



所以我正在制作队列列表,我试图摆脱内存泄漏和未初始化的值。但是当使用valgrind运行时,我不断获得条件跳跃或移动取决于未初始化的值。我试着调试代码并找到错误,但我找不到。

下面是我正在运行的代码:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
struct Node{
string item;
Node* next;
Node* prev;
};
struct Queue{
int size;
Node* head = NULL;
Node* tail = NULL;
};
//Makes queue
Queue* createQueue(){
Queue* n = new Queue;
n->head = NULL;
n->tail = NULL;
n->size = 0;
return n;
}
//checks if empty
bool isEmpty(Queue* List){
if( List->size == 0){
return true;
}else{
return false;
}
}
// add item to queue
bool enqueue(Queue* List, string added){
Node* newy= new Node;
if(List->tail == NULL){
List->head = List->tail = newy;
return true;
}
List->tail->next = newy;
List->tail = newy;
List->size++;
return true;
}
//remove item from queue
string dequeue(Queue* List){
Node* tempo = List->head;
if(List->head == NULL){
return "ERROR";
}
else if (tempo->next !=NULL){
tempo = tempo->next;
return List->head->item;
free(List->head);
List->head = tempo;
}else{
return List->head->item;
free(List->head);
List->head= NULL;
List->tail = NULL;
}
}
// display the queue
void print(Queue* List){
Node* yuuur = List->head;
while(yuuur != NULL){
cout<<(yuuur->item)<<endl;
yuuur = yuuur->next;
}
}
// destroy queue
void destroyQueue(Queue* List){
while(List->head !=NULL){
Node *tempo = List->head;
List->head= List->head->next;
delete tempo;
}
List->tail = NULL;
List->head = NULL;
delete List;
}

我的主测试代码:

//test code
int main(){
Queue* q = createQueue();
cout << boolalpha << isEmpty(q) << endl;
cout << dequeue(q) << endl;
enqueue(q, "Jos");
enqueue(q ,"An");
enqueue(q, "Peter");
print(q);             //Jos, An en Peter worden op drie regels geprint
string first = dequeue(q);
cout << first << endl;  //Jos wordt geprint
print(q);           //An en Peter worden geprint
destroyQueue(q);
return 0;
}

Valgrind错误:

==77== Memcheck, a memory error detector
==77== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==77== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==77== Command: student/labo13
==77==
==77== Conditional jump or move depends on uninitialised value(s)
==77==    at 0x400DDA: print(Queue*) (in /task/student/labo13)
==77==    by 0x401002: main (in /task/student/labo13)
==77==
==77== Conditional jump or move depends on uninitialised value(s)
==77==    at 0x400DDA: print(Queue*) (in /task/student/labo13)
==77==    by 0x40103F: main (in /task/student/labo13)
==77==
==77== Conditional jump or move depends on uninitialised value(s)
==77==    at 0x400DDA: print(Queue*) (in /task/student/labo13)
==77==    by 0x401097: main (in /task/student/labo13)
==77==
==77== Conditional jump or move depends on uninitialised value(s)
==77==    at 0x400E23: destroyQueue(Queue*) (in /task/student/labo13)
==77==    by 0x4010A3: main (in /task/student/labo13)
==77==
==77==
==77== HEAP SUMMARY:
==77==     in use at exit: 0 bytes in 0 blocks
==77==   total heap usage: 10 allocs, 10 frees, 263 bytes allocated
==77==
==77== All heap blocks were freed -- no leaks are possible
==77==
==77== For counts of detected and suppressed errors, rerun with: -v
==77== Use --track-origins=yes to see where uninitialised values come from
==77== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)

enqueue函数中,您可以:

Node* newy= new Node;
if(List->tail == NULL){
List->head = List->tail = newy;
return true;
}

此时,newy是一个刚初始化的Node对象;然而,它的成员都还没有确定。您也不能调整Queue的大小。

其次,假设tail不是NULL,那么你做:

List->tail->next = newy;
List->tail = newy;
List->size++;
return true;

同样,此时newy没有设置任何成员。

你可能想做的是(我没有尝试过):

bool enqueue(Queue* List, const string &added){
Node* newy= new Node;
newy->item = added;
newy->next = nullptr;
newy->prev = List->tail;
List->size++;
if(List->tail == nullptr){
List->head = List->tail = newy;
return true;
}
List->tail->next = newy;
List->tail = newy;
return true;
}

另一个注意事项,你似乎试图像对待C一样对待c++。你应该使用适当的类和成员函数。

相关内容

  • 没有找到相关文章

最新更新