vastbasket.blogg.se

List of list stack exchange
List of list stack exchange




list of list stack exchange

We need to go through the list and delete all the nodes: void free_ll(node *head) That's because we only release the head node of the list the other nodes are still allocated, but no longer reachable. =1612= at 0x48357BF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

list of list stack exchange

Valgrind reports: =1612= 48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2 You need to cast cur to the correct type when using it in printf(), because it's a varargs function. Minor: When a function does not alter objects that are "pointed", consider using const to 1) indicate no change 2) allow passing a pointer to a constant linked list (this is rare with linked lists) void print_ll(const node* head) printf("Node: %d %p\n", cur->val, (void *) cur) Use matching type with the specifier to avoid undefined behavior: void * with "%p". Some_pointer = malloc(sizeof *some_pointer) some_pointer = malloc(sizeof(some_type)) This is less error prone and easier to maintain. Notice how the below malloc() size works without even knowing the type of the pointer. This also handles cases when i next = malloc(sizeof *(cur->next)) we can simply malloc() for each "next" node until the last, setting it equal to NULL. If there's a practical way to write make_ll(). It would probably be noisy to try to reproduce them here. I'm new to C, mostly influenced by Python, and the things I tried to no avail were the result of my ignorance of the languages' difference in scope rules. We can simply malloc() for each "next" node until the last, setting it equal to NULL. I'm wondering if there's a practical way to write make_ll() so that, rather than checking if we've reached the last desired node on every pass of the for loop and setting cur->next accordingly, as done here with line 14: cur->next = (i < (len - 1)) ? malloc(sizeof(node)) : NULL

list of list stack exchange

LIST OF LIST STACK EXCHANGE CODE

I wrote this working code to create a linked list of a given length and print each node's values.






List of list stack exchange