当我尝试在队列中添加元素时,我遇到了分段错误

时间:2017-03-21 14:16:45

标签: c

 30 int main(int argc, char *argv[])
 31 {
 32     Simulation sim = newSimulation();
 33     processCommandSwitches(argc,argv,sim);
 34     getEvent(sim);
 35     runSimulation(sim);
 36     return 0;
 37 }
 38 

*****链接列表和队列功能。当使用ddd时,分段故障发生在第83行

     78 NodeQ *allocNodeQ(Queue q, QElement value)
     79 {
     80     NodeQ *pNew = (NodeQ *)malloc(sizeof(NodeQ));
     81     if(pNew = NULL)
     82         ErrExit(ERR_ALGORITHM, "No available memory for queue");
     83     pNew->element = value;  **//Where i'm getting a segmentation fault**
     84     pNew->pNext = NULL;
     85     return pNew;
     86 }
     87 
    117 
    118 Queue newQueue(char szQueueNm[])
    119 {
    120     Queue q = (Queue) malloc(sizeof(QueueImp));
    121     // Mark the list empty 
    122     q->pHead = NULL;
    123     q->pFoot = NULL;
    124     strcpy(q->szQName, szQueueNm);
    125     q->lQueueWaitSum = 0;
    126     q->lQueueWidgetTotalCount = 0;
    127     return q;
    128 }
    129 
    130 
    131 int removeQ(Queue q, QElement *pFromQElement)
    132 {
    133     NodeQ *p;
    134     //check for empty
    135     if(q->pHead == NULL)
    136         return FALSE;
    137     p = q->pHead;
    138     *pFromQElement = p->element;
    139     q->pHead = p->pNext;
    140 
    141     if(q->pHead == NULL)
    142         q->pFoot = NULL;
    143     free(p);
    144     return TRUE;
    145 }
    146 
    147 void freeQueue(Queue q)
    148 {
    149     NodeQ *p;
    150     NodeQ *pRemove;
    151     for(p=q->pHead; p != NULL; )
    152     {
    153         pRemove = p;
    154         p = p->pNext;
    155         free(pRemove);
    156     }
    157     free(q);
    158 }
    159 
    160 void insertQ(Queue q, QElement element)
    161 {
    162     NodeQ *pNew;
    163     pNew = allocNodeQ(q, element);
    164     // check for empty
    165     if(q->pHead == NULL)
    166     {
    167         q->pHead = pNew;
    168         q->pFoot = pNew;
    169     }
    170     else
    171     {
    172         q->pFoot->pNext = pNew;
    173         q->pFoot = pNew;
    174     }
    175 }
    176 
    177 

我删除了我的链接列表和模拟功能。当我尝试将新元素添加到队列中时,我遇到了分段错误。

1 个答案:

答案 0 :(得分:1)

if(pNew = NULL)你确定吗?你不应该在这里使用==吗?