2016-11-15 50 views
-1

コンパイルは正常ですが、実行時に「セグメンテーションフォルト(コアダンプ)」が発生します。クイックグーグルをやっていると、存在しないメモリにアクセスしようとしているが、どこにいないのか分かりません。以下はセグメンテーションフォルト(コアダンプ)が表示されるのはなぜですか?

 #include "A2Q1.h" 
     using namespace std; 
     int main() { 
     QUEUE *myQ = 0; 
     QUEUE *newQ = 0; 

     myQ = createQueue(myQ); //creates a queue 
     newQ = createQueue(newQ); 

     addElement(myQ, 'L'); 
     addElement(myQ, 'a'); 
     addElement(myQ, 'k'); 
     addElement(myQ, 'e'); 
     addElement(myQ, 'h'); 
     addElement(myQ, 'e'); 
     addElement(myQ, 'a'); 
     addElement(myQ, 'd'); 
     addElement(myQ, ' '); 

     addElement(newQ, 'U'); 
     addElement(newQ, 'n'); 
     addElement(newQ, 'i'); 
     addElement(newQ, 'v'); 
     addElement(newQ, 'e'); 
     addElement(newQ, 'r'); 
     addElement(newQ, 's'); 
     addElement(newQ, 'i'); 
     addElement(newQ, 't'); 
     addElement(newQ, 'y'); 

     catQueue(myQ, newQ); 
     return 0; 
    } 

ヘッダーファイルされる:

#include<stdio.h> 
#include<iostream>//for malloc 

#define MAXQUEUE 25  
typedef char QUEUE_NODE; 

typedef struct Node { 
QUEUE_NODE data;  
struct Node *next; 
}node; 

typedef struct QUEUE { 
int count; 
node *front;  //will point to first element in node 
node *rear;  //points to most recent aded node 
}queue; 

QUEUE* createQueue(QUEUE *q)//pointer to queue created in main 
{ 
q = (QUEUE*)malloc(sizeof(QUEUE)); 
if (q)     //if memory available for q 
{ 
q->front = 0;  //initialize it all to NULL 
q->rear = 0; 
q->count = 0;  //no elements to count yet 
} 
printf("successfully created a queue.\n\n"); 
return q; 
} 

bool addElement(QUEUE *q, char dataPtr) //enqueue element 
{ 

node *newPtr; //contains data & next 

if (!(newPtr = (node*)malloc(sizeof(node)))) 
return false; 
newPtr->data = dataPtr; //letter in dataPtr now placed in queue 
newPtr->next = 0; 
if (q->count == 0) //if this is the first element in queue 
q->front = newPtr; 
else 
q->rear->next = newPtr; 
(q->count)++; //because new element added 
q->rear = newPtr; //new element is placed at rear 
return true; 
} 

bool catQueue(QUEUE *sameQ,QUEUE *addQ) 
{ 
node *tempPtr; //contains data and next 

if (!(tempPtr = (node*)malloc(sizeof(node))))/
return false; 

tempPtr = sameQ->front; //tempPtr points to front of queue 
sameQ->rear->next = addQ->front; 

printf("Concatonated queue:\n\n"); 
while (tempPtr->data != 0) 
{ 
printf("%c", tempPtr->data); 
tempPtr = tempPtr->next; 
} 
return true; 
} 

bool delElement(QUEUE *q, char itemPtr) //dequeue element 
{ 
node *deleteLoc; //has data and next 
if (!q->count) //if no elements execute 
return false; //because queue is empty 
itemPtr = q->front->data; //contains data @ front of queue 
printf("%c", itemPtr); 
deleteLoc = q->front; //points to first node 
if (q->count == 1) 
else 
q->front = q->front->next; 
(q->count)--; 
free(deleteLoc); 
return true; 
} 

int queueCount(QUEUE *q) 
{ 
return q->count; 
} 

QUEUE *destroyQueue(QUEUE *q) //destroys queue 
{ 
node *delPtr; //deletion pointer 

if (q) 
{ 
while (q->front != 0) //while there are still elements in queue 
{ 
    q->front->data = 0; //removes data from node 
    delPtr = q->front; //points to first location in queue 
    q->front = q->front->next; 
    free(delPtr); //deletes current front node 
    } 
    free(q); //deletes entire queue 
    } 
    printf("queue successully deleted\n\n"); 
    return 0; 
    } 

答えて

0

あなたA2Q1.hヘッダcatQueue機能で

の小さなバグがあり...状態で下のCPPファイルがありますループ

あなたのは

tempPtr->data!=0 
ありながら、

それは

tempPtr->next!=0 

する必要があり、あなたはおそらく代わりに

malloc and free 

new and delete 

演算子を使用する必要があります

関連する問題