0

data.h

struct Sub {
    int n;
    struct Sub *next;
}

struct Super {
    struct Sub *Sub
    void (*addSub)(struct Super *self, struct Sub *subRef);
}

data.c

static void addSub(struct Super *self, struct Sub *subRef) {
    struct Sub *head = self->Sub;

    while(head != NULL) { // throwing segmentation fault somewhere here
        head = head->next;
    }

    // assign subRef once we reach to the end.
}

struct Super *newSuper() {
    struct Super *super = malloc(sizeof(struct Super));
    super->addSub = addSub;
    return super;
}

data_test.c

int main() {
    struct Super *super = newSuper();
    super->addSub(super, malloc(sizeof(struct Sub)));
    return 0;
}

I'm relatively new to C, implemented linked list long time back but can't seem to get my head around the null problem, that's how it used to be. How can I detect the end of the list and add the new value to the end?

halfer
  • 19,824
  • 17
  • 99
  • 186
Developer
  • 924
  • 3
  • 14
  • 30

2 Answers2

0

Your addSub function is not doing the real thing, means it's not adding anything. you are traversing to the null node of the list and loosing the parent node. Better check for head->next != null and then add sub to the end as shown below. (I assume that you want to add subRef to the end of the list)

static void addSub(struct Super *self, struct Sub *subRef) {
    struct Sub *head = self->Sub;

    if(head == NULL)
    {
        self->Sub = sub;
        return;
    }

   while(head->next != NULL) { // throwing segmentation fault somewhere here
       head = head->next;


   head->next = subRef;
   subRef->next = NULL; //This step is only required if subRef->next 
                        //was not set to null before the call
  }
Nitin
  • 145
  • 9
  • nope - this is still going to fail. self->Sub has not been initialized anywhere – pm100 Jan 26 '18 at 22:39
  • i figured out, the problem is with `head->next = subRef;`, it should be `head = subRef` – Developer Jan 27 '18 at 00:26
  • @Developer: what problem ? head = subRef is not correct, my code for addSub and pm100 code for newSuper together should work fine for you. – Nitin Jan 27 '18 at 00:35
  • @Developer: if your problem is solved then please mark my response `answered`. appreciate it. Thanks ! – Nitin Jan 27 '18 at 00:37
  • because after while loop ends, the head will point to `NULL`, and then accessing `head->next` (aka `NULL->next`) is going to crash obviously. Sorry, your answer didn't point to the solution in it's entirety. both answers helped though. however, pm100 response enlightened on the NULL initialization at allocation time. – Developer Jan 27 '18 at 00:39
  • @Developer: when while loop ends (aka: head->next != NULL) head will point to the last element in the list and not NULL. it will never become NULL->next – Nitin Jan 27 '18 at 01:15
0

you have to initialize you new object

struct Super *newSuper() {
    struct Super *super = malloc(sizeof(struct Super));
    super->addSub = addSub;
    super->Sub = NULL;
    return super;
}

malloc'ed memory is not cleared to any value; it contains junk.

you should also have a newSub function to create instance of your Sub objects instead of the caller using raw malloc

pm100
  • 48,078
  • 23
  • 82
  • 145
  • initializing super->Sub = NULL would have still crashed in addSub function. I modified addSub to prevent crash. – Nitin Jan 26 '18 at 22:49
  • thats why you were crashing, you probably ended up crashing somewhere else after adding the assingment i suggest – pm100 Jan 26 '18 at 22:51
  • `super->Sub = NULL;` was also the probable cause. see my comment on the other answer too for a complete solution. – Developer Jan 27 '18 at 00:28
  • and I'm wondering, please enlighten me, if i `malloc` some structure, then all member variables have to be initialized to `NULL`? I was assuming they are `NULL` by default, and if that the case checking `if(sub == NULL)` will crash? – Developer Jan 27 '18 at 00:29
  • second question, is it possible to embed the `Sub` struct inside `Super` while still able to assign `NULL` to `Sub` during the alloc phase? – Developer Jan 27 '18 at 01:09
  • Thanks for the answer. Can you please also look into another question, there's a lot of discussion but you can just ignore all and simply download and review the project for a solution. https://stackoverflow.com/questions/48471963/anonymous-functions-return-dynamically-allocated-values – Developer Jan 28 '18 at 11:14