为什么我会收到Segmentation Fault(核心转储)?

时间:2016-11-15 20:32:31

标签: segmentation-fault c++14

它编译得很好,但在执行时我得到了#34;分段错误(核心转储)"。做一些快速的谷歌搜索告诉我,尝试访问不存在的内存,但我不知道在哪里!以下是CPP文件:

        #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;
    }

1 个答案:

答案 0 :(得分:0)

你的A2Q1.h标题中有一个小错误

在catQueue函数中...... while循环中的条件

你的

tempPtr->data!=0

应该是

tempPtr->next!=0

你应该使用

new and delete

运算符而不是

malloc and free