4

so got this segmentation fault every time I tried to check if a pointer variable is a NULL. The error is from these lines of code in add function:

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

This is the whole code that I have:

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

Can you tell me what's wrong with it? and how to solve that? Thanks a lot.

5
  • 2
    Most likely: it is NULL, and you can't get to NULL->head. Thumbs up for debugging enough to find the offending line! Commented Dec 11, 2017 at 0:45
  • Yea, you're not allocating it... Commented Dec 11, 2017 at 0:46
  • @John3136, but if I delete the if statement. The printf statement above itwhich lead to it->head works fine. Commented Dec 11, 2017 at 0:52
  • @little_birdie, but in the IteratorIntNew function above, I allocate a memory to it. Is it different? Commented Dec 11, 2017 at 0:54
  • @John3136, yeah, I just realize that yesterday. I need to return a pointer, right? and it's either I change it into a function pointer or change the data type into a pointer data type. Thanks for explaining anyway. Commented Dec 11, 2017 at 21:36

3 Answers 3

2

In your add function, the variable n is an uninitialized pointer. So it is not the problem of checking it->head.

Sign up to request clarification or add additional context in comments.

Comments

2

if (it->head == NULL)

This like will crash if it itself is not a valid pointer (such as NULL).

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

This takes an un-initialized pointer n, and dereferences it. The most likely outcome would be a crash.

if I delete the if statement. The printf statement above it which lead to it->head works fine

It is hard to believe you, because the n->val is above the printf, and it would most likely crash before you got to printf.

Comments

1

If you want to print the address use %p and (void *) cast.

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

Also

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

Allocate memory properly in IteratorIntNew(). This is one way:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.