Help with Circular lists

Im trying to implement a circular list.Can anybody please point out the errors.The code is compiling but the list is not getting initialized.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<cstring>

using namespace std;

typedef struct Page_
{
	int number;
	int reference;
	void* data;
	struct Page_ *next;
} Page;

typedef struct CList_ 					// Define a structure for circular lists.
{
	int size;
	int (*match)(const void *key1, const void *key2);
	void (*destroy)(void *data);		//
	Page *head;
}CList;

void clist_init(CList *list) 		//clist_init
{
	cout<<"came to init\n";
	 list->size=0; 										//initialize the list
	list->destroy = NULL;
	list->head = NULL;
	cout<<list->size<<endl;

	cout<<"List Initialized"<<endl;

}

int main()
{
        CList* list=NULL;
         cout<<"Initializing the list\n";
	clist_init(list);
return 0;
}
closed account (D80DSL3A)
You are passing a NULL pointer (declared on line 36) to the clist_init() function (where you call it on line 38).
This NULL pointer is then dereferenced (on lines 25-28) which should cause the program to crash!

Maybe line 36 should be something like: CList* list=new list; instead.
Topic archived. No new replies allowed.