Hello everyone

I have been working on generic C data structure (Doubly Linked List) and I have a question:

------ List.c -----

struct node {
void *data;
struct node *next;
struct node *prev;
};

struct list {
unsigned int length;
struct node *head;
struct node *tail;
};
typedef struct list List;

// I also have functions ;-) and one of them is "init()" which initialize the list.


Now here is my problem, users can define a variable "List *list". Right now list is a BAD POINTER because it is not equal to (Point to) any memory address. Now I want to force users to call "init()" function first before calling any other functions in list.c

How can I do that?
"I thought about having global variable in list.c and make it as a flag "static int flag = 0" and if they call init() i change it to '1' but that does not work well ;-)"

Can someone point me to the right direction.


Thanks
Mark

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

I'm not sure why you need to force the users to call init()- a well commented header file or docs should suffice or is this part of the assignment requirements..

Edited 6 Years Ago by gerard4143: n/a

The only thing which comes to mind is to use handles (aka descriptors). The handle-based code does not expose a struct list in any form. An init function returns an opaque handle instead. Of course it needs to maintain an internal registry of allocated handles and a handle-to-list map.
As an example of such approach look at the file descriptors and their open/read/write/close system calls.

Thanks for the reply

gerard4143: It's not a school project or assignment. All I'm trying to do is to create my own data structure library which it must be generic.

I will use that library for my other projects (Again not a school one :-))

nezachem:
Thanks for your reply. You have a really good point (Idea). But I'm not sure how to implement it (I've never done C programming that way). Can you direct me to any documentation / tutorial on how to do it (OR explain more)?

Again thanks a lot for all your help.
Mark

Edited 6 Years Ago by markfisher: n/a

Thanks for the reply

gerard4143: It's not a school project or assignment. All I'm trying to do is to create my own data structure library which it must be generic.

I will use that library for my other projects (Again not a school one :-))

nezachem:
Thanks for your reply. You have a really good point (Idea). But I'm not sure how to implement it (I've never done C programming that way). Can you direct me to any documentation / tutorial on how to do it (OR explain more)?

Again thanks a lot for all your help.
Mark

According to my understanding write your init() function. now write another function my_malloc() in that my_malloc function u call your init() function. whenever user wantto allocate memory to list he will call my_malloc() function. i think that will solve your problem.

Quick and dirty:

static struct List * list_array[MAX_LISTS];

int create_list()
{
    int i;
    for(i = 0; (i < MAX_LISTS) && (list_array[i] != 0); i++)
        ;
    if(i == MAX_LISTS)
        return -1;
    list_array[i] = init_list(); // This is your regular init_list()
    return i;
}

int delete_list(int id)
{
    if(id < 0 || id >= MAX_LIST)
        return -1;
    struct List * list = list_array[i];
    if(list == 0)
        return -1;
    really_delete_list(list); // Your regular memory deallocation etc
    list_array[id] = 0;
    return 0;
}

// etc

Edited 6 Years Ago by nezachem: n/a

Thanks for your reply nezachem
As I can see in your code, you instead of giving users list *, you will give them an id, and list.c keeps track of those ids. id one of them is zero they need to allocate it.
Am I right?

Now here is a problem:
I can call delete_list without allocating. Users are not forced to use create_list fuction and I want to force them in order to use other functions, they must call create_list for every List type variable they created.


Thanks
Mark

When they pass in the list address to delete, if it hasn't been initialized, don't delete it. Return an error code.

This question has already been answered. Start a new discussion instead.