1

I want to initial the sequence list by using a second rank pointer,which point to the STRUCT I made for the sequence list. I tried , the script can be compiled to an executable file ,but fail to operate.

I am using C with the DEV CPP 5.11 as the IDE.

I just want to use the *Sqlist as my parameter to initialize a sequence list...

here is the sequence list .

/* can be compiled ,but fail to execute.*/
#include <stdio.h>
#include <stdlib.h>
#define LISTSIZE 10
typedef int ElemType;
typedef struct List{
    ElemType *elem;
    int length;
    int listsize;  
}List,*Sqlist;

int InitList(Sqlist *L){   
    (*L)->elem=(ElemType*)malloc(sizeof(ElemType)*LISTSIZE);
    if (!(*L)->elem)  return -1;
    (*L)->length=0;
    (*L)->listsize=LISTSIZE;
}
int main(){
    Sqlist La;
    InitList(&La);
}

And this is comfusing compared with the linklist I made with a second rank pointer as the paramenter of the Initialize function.

#include <stdlib.h> 
#include <stdio.h>
#include <typeinfo.h>
typedef int ElemType ;
typedef struct LNode{
    ElemType data;
    struct LNode* next; 
}LNode,*LinkList; 

int InitList(LinkList *L) {
    (*L)=(LinkList)malloc(sizeof(struct LNode));
    if (!*L) return -1;
    (*L)->data= 0;
    (*L)->next =NULL;
    printf("successfully initialized.\n");
    return 0;
}

I really appreciate your help!

gsamaras
  • 71,951
  • 46
  • 188
  • 305
justinRen
  • 27
  • 1
  • 4
  • Don't you see the difference? In the linked list, you allocate space for the node itself and then fill it. In you sequence list, you allocate to `(*L)->elem`, although `*L` doesn't exist yet. – M Oehm Sep 16 '17 at 18:04
  • (I find the practice of allocating a dummy node for an empty list questionable, too. A linked list is a list that has no nodes and it can be initialised when it is defined: `List *La = NULL;`) – M Oehm Sep 16 '17 at 18:07

1 Answers1

0

Notice how the second code does allocate dynamically space for the list, before accessing its members:

(*L)=(LinkList)malloc(sizeof(struct LNode));
(*L)->data= 0;

Now you go and do:

Sqlist La;
InitList(&La);

which will execute this:

int InitList(Sqlist *L) {   
    (*L)->elem=(ElemType*)malloc(sizeof(ElemType)*LISTSIZE);

But there is no memory for the pointer! Where does it point? Nobody knows for sure. You dereference the pointer, ask for its member elem, and boom! You just invoked Undefined Behavior, for sure!

the code compiles but fails to operate.

That's way your program "won't operate", since you have a logical error, as mentioned above.

You first need to allocate memory for Sqlist, as you did for LinkList before, and then access its members.


PS: It's good practice to initialize pointers to NULL, like this: Sqlist La = NULL;.

Oh and by the way, unrelated to your problem, Do I cast the result of malloc? No!

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    thank you for your help:D, I realise that I did not allocate a space for the L itself,so I add " (*L) =(Sqlist)malloc(sizeof(Sqlist)); " before in the Initial function of my Sequence list , and it works! PS:this is my first question in stack overflow ,I am so exciting when someone did notice my question and answer it !Thanks again for your help! – justinRen Sep 17 '17 at 02:48
  • Great @justinRen! Well here when an answer helps, we *accept* it, with the tick mark on the left of my answer. – gsamaras Sep 18 '17 at 05:15