Rami Raghfan
Rami Raghfan

Reputation: 163

How to properly initialize a big structure with doubly linked list?

I am practicing linked lists by attempting to make a simple school structure, I keep error checking as I go and noticed this:

$ gcc list.c -Wall
list.c: In function ‘newStudent’:
list.c:52:15: warning: variable ‘newNode’ set but not used [-Wunused-but-set-variable]
  studentList *newNode;
               ^~~~~~~
list.c: In function ‘initLists’:
list.c:36:14: warning: ‘auxSt’ is used uninitialized in this function [-Wuninitialized]
  auxSt->name = NULL;
              ^
list.c:41:14: warning: ‘auxR’ is used uninitialized in this function [-Wuninitialized]
  auxR->names = NULL;
              ^
list.c:46:16: warning: ‘school’ is used uninitialized in this function [-Wuninitialized]
  school->rooms = NULL;
                ^


This is my code so far...

typedef struct roomList roomList;
typedef struct school school;
typedef struct studentList studentList;

struct studentList
{

    char *name;
    int  grade;
    studentList *next;
    studentList *prev;
};

struct roomList
{   
    int class;
    int nrOfStudents;
    studentList *names;
    roomList *next;
    roomList *prev; 
};

struct school
{
    int totalStudents;
    roomList *rooms;
};

void initLists()
{
    studentList *auxSt;
    auxSt->name = NULL;
    auxSt->next = auxSt->prev = NULL;
    auxSt = NULL;

    roomList *auxR;
    auxR->names = NULL;
    auxR->next = auxR->prev = NULL;
    auxR = NULL;

    school *school;
    school->rooms = NULL;
    school = NULL;
}


int main()
{
    initLists();

     return 0;
}

But it appears that after I initialize my aux variables first with NULL before going to the members this warning disappears. But I have concerns over initializing the aux variables with NULL before its own members. Am I on the right path for this?

What is the advantage of using an init()function over initializing as you're inserting? Haven't seen almost a single example online with this function.

Upvotes: 1

Views: 62

Answers (2)

klutt
klutt

Reputation: 31459

Let's have a look at the first function.

studentList *newStudent(int class, char *name)
{
    studentList *newNode;
    newNode = (studentList*)malloc(sizeof(studentList));

    return NULL;     
}

You are not returning anything. Well, you return NULL pointer. The whole function is equivalent to

studentList *newStudent(int class, char *name)
{
    return NULL;     
}

with the only exception that your code causes an extra memory leak. You need to replace return NULL with return newNode. Also, never cast malloc and use the var instead of type as arg for sizeof. Just use newNode = malloc(sizeof(*newNode))

The function initLists has similar problems. You create a couple of variables, but you don't use them in a way that matters after the function is done. You also try to dereference unallocated pointers.

Am I on the right path for this?

Not really. I would suggest studying pointers and memory allocation a bit more, and search for example code for doubly linked lists.

Upvotes: 2

Jabberwocky
Jabberwocky

Reputation: 50912

studentList *auxSt;  // auxSt is not initialized
auxSt->name = NULL;  // here you dereference a non initialized pointer

Of course you get ‘auxSt’ is used uninitialized in this function warning here, what do you expect?

Now I guess you tried this:

studentList *auxSt = NULL;  // auxSt is initialized to NULL
auxSt->name = NULL;         // here you dereference an initialized pointer (no warning)

Here you don't get a ‘auxSt’ is used uninitialized in this function warning, because here auxSt is initialized. However this code is still wrong because dereferencing a NULL pointer isn't going to end well.

So globally you're probably not on the right path here.

The whole initLists function is totally wrong.

Upvotes: 0

Related Questions