c - malloc and pointer in a struct -


i have following c code:

typedef struct dlistnode_ {     void    *data;     struct dlistnode_ *prev;     struct dlistnode_ *next; } dlistnode;   typedef struct dlist_ {     int size;     dlistnode  *tail;     dlistnode  *head; } dlist;  void insert(dlist * list, dlistnode * element, int data) {     dlistnode * new_element = (dlistnode *)malloc(sizeof(dlistnode));     new_element->data = &data;     if (list->head==null) {         list->head=list->tail=new_element;         list->size++;         return;     }     if(element == null) {         // handle size==0?         new_element->next=list->head;         list->head->prev=new_element;         list->head=new_element;         list->size++;     } else {         printf("not yet implemented!\n");     } }  void printnodes(dlist *list) {     dlistnode * pointer = list->head;     if (pointer!=null) {         int v= *((int*)pointer->data);         printf("node has value: %d\n", v);         while (pointer->next != null) {             v = *((int*)pointer->data);             printf("node has value: %d\n", v);             pointer=pointer->next;         }     } }  int main(int argc, const char * argv[]) {      int e0 = 23;     int e1 = 7;     int e2 = 11;     dlist *list = (dlist *)malloc(sizeof(dlist));     initlist(list);     assert(count(list)==0);     insert(list, null, e0);     assert(count(list)==1);      insert(list,null, e1);     assert(count(list)==2);      insert(list,null, e2);     assert(count(list)==3);     printnodes(list);      return 0; } 

i have few problems:

  1. does dlistnode * new_element = (dlistnode *)malloc(sizeof(dlistnode)); allocate space the, data, prev, next pointer or manually need call malloc on each of pointers?
  2. when print content of data pointer in each node have value 3 though insert 23, 7 , 11 , set data pointer address of int: ** new_element->data = &data;**.

(introductionary textbooks on c have been ordered)

edit:

insert takes void pointer data:

// insert data new head void insert(dlist *list, dlistnode *element, void *data) {     dlistnode *new_element = malloc(sizeof(dlistnode));     new_element->data = data;     if (list->head==null) {         list->head=list->tail=new_element;         list->size++;         return;     }     if(element == null) {         new_element->next=list->head;         list->head->prev=new_element;         list->head=new_element;         list->size++;     } else {         printf("not yet implemented!\n");     } } 

in main do:

int main(int argc, const char * argv[]) {     int i0=7;     int *ip0 = malloc(sizeof(int));     ip0 = &i0;      int i1=8;     int *ip1 = malloc(sizeof(int));     ip1 = &i1;      int *ip2 = malloc(sizeof(int));     int i2=44;     ip2 = &i2;      dlist *list = malloc(sizeof(dlist));     initlist(list);     // create nodes     assert(count(list)==0);     insert(list, null, ip0);     assert(count(list)==1);      insert(list,null, ip1);     assert(count(list)==2);      insert(list,null, ip2);     assert(count(list)==3);     printnodes(list);      return 0; } 

which outputs:

node has value: 44 node has value: 44 node has value: 8 

but should be:

node has value: 44 node has value: 8 node has value: 7 

  1. malloc(sizeof(dlistnode)) allocates space 1 dlistnode, definition consists of void* , 2 dlistnode pointers. not initialize pointers, though.

  2. you're assigning address of data argument insert. that's pointer temporary invalidated once insert returns. behavior of program undefined. easy solution replace void *data int data.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -