0

I am trying to code an easy list in C, which is able to store numbers.
With number SIZE, an index will be calculated and the numbers have to be stored in a kind of linear list at the array index.
However, sometimes I get a "segmentation fault", like 2 of 10 tries the output is correct.
I've gone a long search, but I couldn't find the problem.
Please keep in mind that my "solution" isn't implemented fully, so currently it's only working when the calculated index has no pointer thats stored. (Couldn't continue to code because of the error.)

Here is my code:

#define SIZE 3
#define NULL 0

typedef struct node_s node_t;

struct node_s {
  node_t* next;
  int number;
};

static node_t nodeArray[SIZE];

int addNumber(int numb){
  int index = number % SIZE;
  if(nodeArray[index].next == NULL){
    node_t* node = (node_t*) malloc(sizeof(node_t));
    node->number = number;
    nodeArray[hash].next = node;
  }
}

void print(){
  for(int i = 0; i < SIZE; i++){
    node_t* ptr = nodeArray[i].next;
    while(ptr != NULL){
      printf("%d -> ", ptr->number);
      ptr = ptr->next;
    }
    printf("\n");
  }
}

#include "dictionary.h"
#include <stdio.h>

int main(){
  insert(1);
  insert(2);
  print();
  return 0;
}

What causes the "segmentation fault" sometimes? I appreciate any kind of help, thanks in advance!

8
  • 1
    this code can't cause anything because it doesn't have main. Commented Nov 15, 2017 at 17:12
  • 1
    you don't need to/shouldn't define your own NULL. What's hash? I assume numb in the addNumber function is supposed to be number? But to answer your question, invoking undefined behavior is what causes segmentation fault sometimes. Commented Nov 15, 2017 at 17:13
  • 1
    Make sure to post the code that compiles and actually segfaults in its entirety, not some edited version. The code you posted will not compile, as hash variable is not defined. You should also check the return value of malloc() for error. Commented Nov 15, 2017 at 17:14
  • 1
    Where do you init the member next before you do ptr=ptr->next;? I can't see it anywhere near malloc. Commented Nov 15, 2017 at 17:14
  • 2
    Code shown also doesn't declare "hash" anywhere. Clearly you're not posting the code you're using. Commented Nov 15, 2017 at 17:17

1 Answer 1

5

Just after malloc you do init one member.

node->number = number;

But you do not init the other member, there is no

node->next = NULL;

Also, in your loop condition inside print(), you check ptr against NULL, but that is in most of the looping the non-initialised ptr->next from previous loop.

ptr = ptr->next;

I.e. you rely on it to be initialised to NULL.
That is probably causing the segfault.

Useful background, as pointed out by yano (thanks):
Malloc doesn't initialize memory to 0. In order to do that you can malloc followed by a memset or you can use calloc.

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

3 Comments

Thanks, this is the solution for me. However i thought its automaticly initialised with 0, but that clearly wasnt the case. Thank you.
@Tjatte no, malloc doesn't initialize memory to 0. In order to do that you can malloc followed by a memset or you can use calloc
Thanks for the explanation, i will keep that in mind.

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.