2017-03-21 41 views
0
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 

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

+0

如果您在调试器中运行您的代码,它会告诉您确切发生段错误的位置,并且您将能够打印出变量必须知道为什么它失败的值 –

回答

1

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

+0

哇谢谢! – SamBank

相关问题