【问题标题】:C - Segmentation fault when checking if a pointer is NULLC - 检查指针是否为 NULL 时出现分段错误
【发布时间】:2017-12-11 00:42:02
【问题描述】:

所以每次我尝试检查指针变量是否为NULL 时都会出现此分段错误。错误来自 add 函数中的这些代码行:

if (it->head == NULL){
        printf("worksfine");
    }

这是我拥有的全部代码:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef struct Node{
    int val;
    struct Node *prev;
    struct Node *next;
} node;

typedef struct IteratorInt{
    node *head;
    node *tail;
    node *last;
} IteratorInt;

IteratorInt IteratorIntNew(){
    IteratorInt *listint;
    listint = malloc(sizeof(IteratorInt));
    listint->head = NULL;
    listint->tail = NULL;
    listint->last = NULL;
    printf("address made %u\n", listint);
    return *listint;
}

int add(IteratorInt *it, int v){
    node *n;
    n->val = v;
    n->next = NULL;
    n->prev = NULL;
    printf("func works\n");
    printf("n %d\n", n->val);
    printf("address %u", it);
    it->head = n;
    printf("result %d", it->head->val);
    if (it->head == NULL){
        printf("worksfine");
    }
   /* if (it->head == 0){
       it->head = n;
    }
    if (it->tail == 0){
        it->tail = n;
    }
    if (it->last == 0){
        it->last = n;
    }*/

    return 1;
}

int main() {
    IteratorInt lit = IteratorIntNew();
    printf("works %u", &lit);
    add(&lit, 10);

    /*printf("Node value %d\n", lit.head.val);
    add(&lit, 15);
    printf("Node value %d", lit.tail.val);*/
    return 0;
}

你能告诉我它有什么问题吗?以及如何解决?非常感谢。

【问题讨论】:

  • 很可能:it 为 NULL,您无法访问 NULL-&gt;head。竖起大拇指调试足以找到有问题的行!
  • 是的,你没有分配it...
  • @John3136,但如果我删除 if 语句。它上面的 printf 语句导致it-&gt;head 工作正常。
  • @little_birdie,但是在上面的IteratorIntNew函数中,我给它分配了一块内存。不一样吗?
  • @John3136,是的,我昨天才意识到这一点。我需要返回一个指针,对吧?要么我将其更改为函数指针,要么将数据类型更改为指针数据类型。无论如何,感谢您的解释。

标签: c segmentation-fault


【解决方案1】:

在您的add 函数中,变量n 是一个未初始化的指针。所以不是查it-&gt;head的问题。

【讨论】:

    【解决方案2】:

    if (it-&gt;head == NULL)

    如果it本身不是一个有效的指针(例如NULL),这个like会崩溃。

    int add(IteratorInt *it, int v){ node *n; n->val = v;

    这需要一个未初始化指针n,并取消引用它。最可能的结果是崩溃。

    如果我删除 if 语句。它上面的 printf 语句导致它->head 工作正常

    很难相信你,因为n-&gt;val 高于printf,并且它很可能会在之前崩溃你必须到printf。

    【讨论】:

      【解决方案3】:

      如果要打印地址,请使用 %p 和 (void *) cast。

      printf("address made %p\n", (void *) listint);
      printf("address %p",(void *)  it);
      printf("works %p",(void *)  &lit);
      

      还有

      node *n; // - is not initialized 
      it->head = n;
      printf("result %d", it->head->val); // will print garbage
      

      在IteratorIntNew() 中正确分配内存。这是一种方式:

      #include<stdio.h>
      #include<stdlib.h>
      #include<assert.h>
      
      typedef struct Node{
          int val;
          struct Node *prev;
          struct Node *next;
      } node;
      
      typedef struct IteratorInt{
          node *head;
          node *tail;
          node *last;
      } IteratorInt;
      
      IteratorInt *IteratorIntNew(){
          IteratorInt *listint;
          listint = malloc(sizeof(IteratorInt));
          listint->head = NULL;
          listint->tail = NULL;
          listint->last = NULL;
          printf("address made %p\n", (void *) listint);
          return listint;
      }
      
      int add(IteratorInt *it, int v){
          node *n;
          n->val = v;
          n->next = NULL;
          n->prev = NULL;
      
          printf("func works\n");
          printf("n %d\n", n->val);
          printf("address %p",(void *)  it);
      
          it->head = n;
          printf("result %d", it->head->val);
      
          if (it->head == NULL){
              printf("worksfine");
          }
      
         /* if (it->head == 0){
             it->head = n;
          }
          if (it->tail == 0){
              it->tail = n;
          }
          if (it->last == 0){
              it->last = n;
          }*/
      
          return 1;
      }
      
      int main() {
          IteratorInt *lit = IteratorIntNew();
          printf("works %p",(void *)  lit);
          add(lit, 10);
      
          /*printf("Node value %d\n", lit.head.val);
          add(&lit, 15);
          printf("Node value %d", lit.tail.val);*/
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2022-06-12
        • 2020-10-04
        • 2021-04-10
        • 1970-01-01
        • 2020-09-26
        • 1970-01-01
        • 2021-12-23
        • 2020-02-18
        • 2016-03-20
        相关资源
        最近更新 更多