2017-12-11 19 views
1

ポインタ変数がNULLかどうかをチェックしようとするたびに、このセグメンテーションフォルトが得られました。エラーは、追加機能のコードのこれらの行から次のとおりです。C - ポインタがNULLかどうかをチェックする際のセグメンテーションエラー

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

あなたはそれで間違っているものを私に言うことはできますか?それを解決する方法は?どうもありがとう。

+1

ほとんどの場合: 'it'がNULLで、そしてあなたが' nullで> head'に取得することはできません。問題のある行を見つけるのに十分なデバッグをお勧めします! – John3136

+0

さて、あなたはそれを割り当てていません... –

+0

@ John3136、if文を削除します。上記の 'it-> head'につながるprintf文は正常に動作します。 – garjted

答えて

2

add関数では、変数nは初期化されていないポインタです。したがって、it->headをチェックするのは問題ではありません。

1

if (it->head == NULL)

it自体は(例えばNULLなど)有効なポインタでない場合、このようにクラッシュします。

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

これは初期化されていないポインタn、および逆参照それを取ります。最も可能性の高い結果はクラッシュです。

if文を削除した場合。 IT-につながる、それ以上のprintf文は>ヘッドはあなたが得た

n->valprintfの上にあるので、それはあなたを信じるように難しいで、それだろう最も可能性の高いクラッシュ前に、正常に動作しますprintf

0

アドレスを印刷する場合は、%pと(void *)キャストを使用してください。

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()に適切にメモリを割り当てます。これは、1つの方法です:

#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; 
} 
関連する問題