用C分段错误 [英] Segmentation Fault Error in C

查看:126
本文介绍了用C分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是为C上新建一个程序员,我在链表的程序得到分段错误,我想知道是否有人可以帮助我。我已为我下面code ...如果你需要furthure信息,我将它张贴。谢谢你。

 的#includelist.h// + ----------------------------------------------- --------------
// +创建节点
// +
// +分配内存类型结构节点的节点,
// +与D初始化。返回一个指针到新节点。
// + ----------------------------------------------- --------------
结构节点* createNode(INT D){
  结构节点* newNode =的malloc(sizeof的(结构节点)); //创建和一个名为newNode新节点分配内存空间  newNode->项目= D; // newNode的数据存储在D的价值
  newNode->接着= NULL; //设置指针到下一个节点为NULL  返回newNode; //返回创建的新节点
}// + ----------------------------------------------- --------------
// + INSERT头节点
// +
// +插入节点n在列表中的头前,并设置
// + N是列表的新掌门人。
// + ----------------------------------------------- --------------
无效insertHead(结构节点** headRef,结构节点* N){
  结构节点* newNode =的malloc(sizeof的(结构节点)); //创建和一个名为newNode新节点分配内存空间  newNode->项目= N->项目; // newNode的数据被分配的参数节点n'的值
  newNode->接下来= * headRef; //因为我们是在头插入节点,我们设定的下一个节点是头参考
  * headRef = newNode; //然后我们分配头参考创建的,因此新的节点,插入头节点
}// + ----------------------------------------------- --------------
// + INSERT尾节点
// +
// +插入节点n在链表的尾部。
// + ----------------------------------------------- --------------
无效insertTail(结构节点** headRef,结构节点* N){
  结构节点* newNode =的malloc(sizeof的(结构节点)); //创建和一个名为newNode新节点分配内存空间
  newNode = * headRef; //现在新的节点是头参考  而(newNode->!下次= NULL)//而下一个节点不等于空
 newNode = newNode->接下来, //设置newNode到下一个节点(在此发现的最后一个节点)  结构节点* TMP =的malloc(sizeof的(结构节点)); //创建和一个名为TMP新节点分配内存空间  tmp->项目= N->项目; // tmp中的数据被分配参数节点的数据'N'
  tmp->接着= NULL; //节点以下TMP设置为NULL
  newNode->接下来= TMP; // TMP现在设置到下一个节点,由此,成为最后一个节点,即尾部
}// + ----------------------------------------------- --------------
// + COUNT在链接列表中的节点
// +
// +计数是LinkedList的一部分的节点的#。
// + ----------------------------------------------- --------------
INT countNodes(结构节点* headRef){
  INT计数器= 0; //创建计数器变量来存储节点的数目  结构节点*电流= headRef; //创建一个新的节点,并为它分配参考头节点  如果(headRef = NULL)返回0; //如果头是NULL,返回0(如果没头没节点)  而(电流!= NULL){//而当前节点不是NULL
 反++; //增加计数器
 电流=电流 - >接着, //并移动到下一个节点,从而,将1加到与传递的每个节点的计数器
  }
  返回柜台; //返回节点的总数,存储在计数器
}// + ----------------------------------------------- --------------
// + FIND NODE
// +
// +返回具有项目= VAL,返回NULL的第一个节点
// +其他。
// + ----------------------------------------------- --------------
结构节点* findNode(结构节点*头,诠释VAL){
  结构节点* TMP =的malloc(sizeof的(结构节点)); //创建和一个名为TMP新节点分配内存空间  * TMP = *头; //节点TMP现在参照列表的头节点  而(TMP!= NULL)//而TMP节点不等于NULL
  {
 如果(tmp->项== VAL){//如果TMP节点的数据是相等的值被作为参数
  返回TMP; //返回TMP节点
 }其他{
  返回NULL; //否则,返回NULL
 }
   TMP = tmp->接下来, //设置TMP节点到下一个节点列表中(遍历)
  }
}// + ----------------------------------------------- --------------
// + DELETE NODE
// +
// +删除节点n从列表中分配到n释放内存。
// + ----------------------------------------------- --------------
无效deleteNode(结构节点** headRef,结构节点* N){
 结构节点* toBeDeletedNode =的malloc(sizeof的(结构节点)); //创建和新的节点称为toBeDeletedNode分配内存空间 toBeDeletedNode = findNode(* headRef,正>的项目); // toBeDeletedNode被设定为等于节点findNode()返回
       //这个节点应该是节点,其数据=的参数节点n的数据
 免费(toBeDeletedNode); //删除节点toBeDeletedNode引用和释放空间分配它
}

下面是测试文件....

 的#includelist.h
#包括LT&;&ASSERT.H GT;
#包括LT&; SYS / types.h中>
#包括LT&;&stdio.h中GT;//创建和insertHead
TEST1无效()
{
 结构节点* headRef = NULL;
    结构节点* NPTR = NULL;
 INT H = 0; 而(八小于5)
      insertHead(安培; headRef,createNode(H +));
 H = 0;
 为(NPTR = headRef;!NPTR = NULL; NPTR = nptr->接着,H +)
  断言(nptr->项目==(4 - h)条);
 断言(H == 5); 的printf(哈哈);
}//创建和insertTail
空TEST2()
{
 结构节点* headRef = NULL;
    结构节点* NPTR = NULL;
 INT H = 0,t = 0时; 而(八小于5)
      insertTail(安培; headRef,createNode(H +)); H = 0;
 为(NPTR = headRef;!NPTR = NULL; NPTR = nptr->接着,H +)
  断言(nptr->项目== H);
 断言(H == 5);
 的printf(哈哈);
}// countNodes
无效TEST3()
{
 结构节点* headRef = NULL;
    结构节点* NPTR = NULL;
 INT H = 0,t = 0时; 而(H< 50)
      insertTail(安培; headRef,createNode(H +));
 H = 0;
 为(NPTR = headRef;!NPTR = NULL; NPTR = nptr->接着,H +)
  断言(nptr->项目== H);
 断言(countNodes(headRef)== 50);
}// findNode
无效TEST4()
{
 结构节点* headRef = NULL;
    结构节点* NPTR = NULL;
 INT H = 0; NPTR = findNode(headRef,1);
 断言(NPTR == NULL); 而(H< 50)
      insertTail(安培; headRef,createNode(H +));
 NPTR = findNode(headRef,10);
 断言(NPTR!= NULL);
 断言(nptr->项目= 10); NPTR = findNode(headRef,-10);
 断言(NPTR == NULL);
}// deleteNode
无效TEST5()
{
 结构节点* headRef = NULL;
    结构节点* NPTR = NULL;
 INT H = 0; 而(八小于5)
      insertTail(安培; headRef,createNode(H +)); H = 0;
 而(八小于5){
  NPTR = findNode(headRef,H);
  断言(NPTR!= NULL);
  deleteNode(安培; headRef,NPTR);
  断言(findNode(headRef,H)== NULL);
  断言(countNodes(headRef)==(4 - h)条);
  ^ h ++;
 }
}/ * //排序
无效TEST6()
{
 结构节点* headRef = NULL;
    结构节点* NPTR = NULL;
 INT H = 0;
 INT D [5] = {1,0,-1,5,100};
 INT DS [5] = {-1,0,1,5,100}; 而(八小于5)
      insertTail(安培; headRef,createNode(D [H +]));   排序(安培; headRef); H = 0;
 为(NPTR = headRef;!NPTR = NULL; NPTR = nptr->接着,H +)
  断言(nptr->项目== DS [H]);
} * /INT主(INT ARGC,字符** argv的)
{
    INT testNum = 0;    如果(的argc 2){
        fprintf中(标准错误,\\ n用法:%s的测试NUM \\ n,argv的[0]);
        返回1;
    }   testNum =的atoi(ARGV [1]);
   开关(testNum){
        情况1:
                TEST1();
                打破;
        案例2:
                TEST2();
                打破;
        案例3:
                TEST3();
                打破;
        情况4:
                TEST4();
                打破;
        情况5:
                TEST5();
                打破;
        情况6:
                // TEST6();
                打破;        默认:
                fprintf中(标准错误,\\ n用法:%s的1。8 \\ N的argv [0]);
                 返回1;
   }
   返回0;
}


解决方案

我不知道这是错误的,但此行几乎肯定是错的:

 如果(headRef = NULL)返回0; //如果头是NULL,返回0(如果没头没节点)

和应

 如果(headRef == NULL)返回0; //如果头是NULL,返回0(如果没头没节点)

这是在 countNodes()

Hi i'm a new programmer for C, i get a segmentation fault in my linked list program, i was wondering if anyone could help me out. I've posted my code below... If you need furthure information I will post it. Thanks.

#include "list.h"

//+-------------------------------------------------------------
//+ CREATE NODE
//+
//+ Allocate memory for a node of type struct node and 
//+ initialize it with d. Return a pointer to the new node.
//+-------------------------------------------------------------
struct node* createNode(int d){
  struct node* newNode = malloc(sizeof(struct node)); //create and allocate space in memory for a new node called newNode

  newNode->item = d; //newNode's data is the value stored in 'd'
  newNode->next = NULL; //sets the pointer to the next node to NULL

  return newNode; //return the new node created
}

//+-------------------------------------------------------------
//+ INSERT HEAD NODE
//+
//+ Insert Node n in front of the head of the list, and set
//+ n to be the new head of the list.
//+-------------------------------------------------------------
void insertHead(struct node **headRef, struct node *n){
  struct node* newNode = malloc(sizeof(struct node)); //create and allocate space in memory for a new node called newNode

  newNode->item = n->item; //newNode's data is assigned the value of the parameter node n''
  newNode->next = *headRef; //since we are inserting the node at the head we set the next node to be the head reference
  *headRef = newNode;   //and then we assign the head reference to the new node created, thus, inserting the head node
}

//+-------------------------------------------------------------
//+ INSERT TAIL NODE
//+
//+ Insert Node n at the tail of the LinkedList.
//+-------------------------------------------------------------
void insertTail(struct node **headRef, struct node *n){
  struct node* newNode = malloc(sizeof(struct node)); //create and allocate space in memory for a new node called newNode
  newNode = *headRef;   //the new node is now the head reference

  while(newNode->next != NULL)  //while the next node is not equal NULL
 newNode = newNode->next; //set the newNode to the next node (this finds the last node)

  struct node* tmp = malloc(sizeof(struct node)); //create and allocate space in memory for a new node called tmp

  tmp->item = n->item;   //the data of tmp is assigned the data of the parameter node 'n'
  tmp->next = NULL;   //the node following tmp is set to NULL
  newNode->next = tmp;   //tmp is now set to the next node, thus, becoming the last node i.e. the tail
}

//+-------------------------------------------------------------
//+ COUNT NODES IN LINKED LIST
//+
//+ Count the # of nodes that are part of the LinkedList.
//+-------------------------------------------------------------
int countNodes(struct node *headRef){
  int counter = 0; //create a counter variable to store the number of nodes

  struct node* current = headRef; //create a new node and assign it the reference to the head node

  if(headRef = NULL) return 0;  //if the head is NULL, return 0 (no nodes if no head)

  while(current != NULL){  //while the current node is not NULL
 counter++;   //increment the counter
 current = current->next; //and move on to the next node, thus, adding 1 to the counter with each node passed
  }
  return counter; //return the total number of nodes, stored in counter
}

//+-------------------------------------------------------------
//+ FIND NODE
//+
//+ Return the first node that has item = val, return NULL 
//+ otherwise.
//+-------------------------------------------------------------
struct node* findNode(struct node *head, int val){
  struct node* tmp = malloc(sizeof(struct node)); //create and allocate space in memory for a new node called tmp

  *tmp = *head;   //node tmp is now referring to the head node of the list

  while(tmp != NULL)  //while the tmp node is not equal to NULL
  {
 if(tmp->item == val){ //if the data of the tmp node is equal to the value sent as parameter 
  return tmp; //return the tmp node
 }else{
  return NULL; //otherwise, return NULL
 }  
   tmp = tmp->next; //set the tmp node to the next node in the list (traversing)
  }
}

//+-------------------------------------------------------------
//+ DELETE NODE
//+
//+ Delete node n from the list and free memory allocated to n.
//+-------------------------------------------------------------
void deleteNode(struct node **headRef, struct node *n){
 struct node* toBeDeletedNode = malloc(sizeof(struct node)); //create and allocate space in memory for a new node called toBeDeletedNode

 toBeDeletedNode = findNode(*headRef, n->item); //toBeDeletedNode is set to equal the node findNode() returns
       //this node should be the node with its data = to the data of the parameter node n
 free(toBeDeletedNode); //delete node toBeDeletedNode references and free the space allocated it
}

Here is the test file....

#include "list.h"
#include <assert.h>
#include <sys/types.h>
#include <stdio.h>

// create and insertHead
void test1() 
{
 struct node *headRef=NULL;
    struct node *nptr = NULL;
 int h=0;

 while(h<5)
      insertHead(&headRef,createNode(h++));
 h = 0;
 for (nptr = headRef; nptr != NULL; nptr = nptr->next, h++) 
  assert(nptr->item == (4 - h) );
 assert(h==5);

 printf("HAHA");
}

// create and insertTail
void test2() 
{
 struct node *headRef=NULL;
    struct node *nptr = NULL;
 int h=0, t=0;

 while(h<5)
      insertTail(&headRef,createNode(h++));

 h = 0;
 for (nptr = headRef; nptr != NULL; nptr = nptr->next, h++) 
  assert(nptr->item ==  h);
 assert(h==5);
 printf("HAHA");
}

// countNodes 
void test3() 
{
 struct node *headRef=NULL;
    struct node *nptr = NULL;
 int h=0, t=0;

 while(h<50)
      insertTail(&headRef,createNode(h++));
 h = 0;
 for (nptr = headRef; nptr != NULL; nptr = nptr->next, h++) 
  assert(nptr->item ==  h);
 assert(countNodes(headRef) == 50);
}

// findNode 
void test4() 
{
 struct node *headRef=NULL;
    struct node *nptr = NULL;
 int h=0;

 nptr = findNode(headRef, 1);
 assert(nptr == NULL);

 while(h<50)
      insertTail(&headRef,createNode(h++));


 nptr = findNode(headRef, 10);
 assert(nptr != NULL);
 assert (nptr->item = 10);

 nptr = findNode(headRef, -10);
 assert(nptr == NULL);
}

// deleteNode 
void test5() 
{
 struct node *headRef=NULL;
    struct node *nptr = NULL;
 int h=0;

 while(h<5)
      insertTail(&headRef,createNode(h++));

 h = 0;
 while(h<5) {
  nptr = findNode(headRef, h);
  assert(nptr != NULL);
  deleteNode(&headRef, nptr);
  assert(findNode(headRef, h) == NULL);
  assert(countNodes(headRef) == (4 - h));
  h++;
 }
}

/*// sort
void test6() 
{
 struct node *headRef=NULL;
    struct node *nptr = NULL;
 int h=0;
 int d[5] = {1, 0, -1, 5, 100};
 int ds[5] = {-1, 0, 1, 5, 100};

 while(h<5)
      insertTail(&headRef,createNode(d[h++]));

   sort(&headRef);

 h = 0;
 for (nptr = headRef; nptr != NULL; nptr = nptr->next, h++) 
  assert(nptr->item ==  ds[h]);
}*/

int main( int argc, char ** argv )
{
    int testNum = 0;

    if ( argc < 2 ) {
        fprintf(stderr, "\n usage: %s test-num\n", argv[0]);
        return 1;
    }

   testNum = atoi(argv[1]);
   switch(testNum){
        case 1:
                test1();
                break;
        case 2:
                test2();
                break;
        case 3:
                test3();
                break;
        case 4:
                test4();
                break;
        case 5:
                test5();
                break;
        case 6:
                //test6();
                break;

        default:
                fprintf(stderr, "\n usage: %s 1 .. 8\n", argv[0]);
                 return 1;
   }
   return 0;
}

解决方案

I don't know if this is the error, but this line is almost certainly wrong:

  if(headRef = NULL) return 0;  //if the head is NULL, return 0 (no nodes if no head)

and should be

  if(headRef == NULL) return 0;  //if the head is NULL, return 0 (no nodes if no head)

It's in countNodes().

这篇关于用C分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆