1

So i have this code, the snippet is given below.

list* init(list* list1)
{
  list1->head = NULL;
  list1->size = 0;
  return list1;
}

list1 is a linked list and init is called from main function. now on the line list1->head= NULL, after i run the code it highlights this particular line and says

No module definition file specified: using defaults.

and it stopd execution.

I am using turbo C on windows 7.

what shall i do? Shall i post the complete code.. It is kinda big though..

Kraken
  • 23,393
  • 37
  • 102
  • 162
  • 1
    Could you please add assert(list1); in the top of the function. – eisbaw Sep 09 '11 at 17:49
  • I suspect we'll need more code to figure it out. That being said, it's possible that NULL is not defined for your compiler - try `list1->head = 0` and see if it complains. – Chris Sep 09 '11 at 17:49
  • @Chris on changing it to `list->head =0` it says, General Protection Exception 0x213F:0X000D Processor fault – Kraken Sep 09 '11 at 17:51
  • @eisbaw list* init(list* list1) { assert(list1) ; list1->head = NULL; list1->size = 0; return list1; } on running gives assertion failed. Probable reasons? – Kraken Sep 09 '11 at 17:55
  • 1
    @Karan if `assert(list1)` failed, that means that you didn't allocate memory for list1 before calling the function. Do you have a `list* list1 = malloc(sizeof(list))` somewhere before this? – Chris Sep 09 '11 at 18:01

2 Answers2

0

NULL is defined in < stddef.h > for C, and the equivalent < cstddef > for C++.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
0

Based on our discussion in the comments it looks like a combination of factors.

First, it appears the Turbo C compiler doesn't know what NULL is. Since NULL is just a typedef for 0, you can use 0 instead.

Second, it seems you haven't allocated memory for the list object. Try doing the following before you call the function.

list* list1 = malloc(sizeof(list));

However, if what you're trying to do is create and initialize a new list object, you're better off rewriting the function as follows:

list* init(){
    list *new_list = malloc(sizeof(list));
    new_list->head = 0; // <-- this sets head to the equivalent of NULL
    new_list->size =0;
    return new_list;
}

If what you want is a function that reinitializes an existing list, then you're causing a memory leak with your current code anyway.

Chris
  • 16,872
  • 1
  • 15
  • 16