The following code implements an operation on a doubly linked list to add one element.
30 struct node {
31 struct node *next;
32 struct node *prev;
33 int* value;
34 };
35
36 // takes pointer to pointer to value and performs operations on value
37 void add_value(struct node ** valuep , int* v){
38 // printf (" addnull %d v %d"
39 if(valuep == NULL ){
40 struct node **v = malloc(sizeof **v);
41 valuep = v;
42
43 }
44 if( (* valuep) == NULL ){
45 // printf (" valuep null %d",*v);
46 // set new value
47
48 struct node *val = malloc(sizeof *val);
49 *valuep = val;
50 val -> next = NULL;
51 val -> prev = NULL;
52 val -> value = v;
53 } else if( (*valuep) -> next == NULL){
54 // printf (" valuep next null %d v %d\n" ,*(* valuep)->value ,*v);
55 struct node *val = malloc(sizeof *val);
56 (* valuep)->next = val;
57 val -> next = NULL;
58 val -> prev = *valuep;
59 val -> value = v;
60 // printf (" valuep %d v %d\n" ,*(* valuep)->value , *v);
61 // printf (" val %d v %d\n",*val ->value , *v);
62 // ent -> values = value;
63 // add_value(ent , value );
64
65 } else {
66 add_value( &(* valuep)->next , v);
67 }
68
69 }
70 ...
Questions
(a) Does the code compile without errors (Yes / No)? 4 marks
(i) If you answered YES, what is wrong with the function add value? annotate the code with brief
comments and arrows, circles etc.
(ii) If you answered NO, what syntax needs to be corrected to make the code compile? annotate the
code with brief comments and arrows, circles etc.
Error message - 4 marks
Any potential syntax errors were removed, and the program was run with the test data. The following
message is shown:
ASAN:DEADLYSIGNAL
- =================================================================
- ==6==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x0000004ea9e9 bp 0x7ffd530238f0 - #0 0x4ea9e8 in add_value /home/linked-data.c:53:26
- #1 0x4ec40c in main /home/linked-data.c:589:7
- #2 0x7f512bd6d82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
- #3 0x4185e8 in _start (/home/linked-data+0x4185e8)
- AddressSanitizer can not provide additional info.
- SUMMARY: AddressSanitizer: SEGV /home/linked-data.c:53:26 in add_value
- ==6==ABORTING
Describe what the error means, what offending value is present in the stack, and how to fix it.
Memory size - 6 marks
(b) 2 marks Describe what the operator sizeof is achieving in the context of the code on lines 40, 48,
and 55.
(c) 1 mark Given that this program was run on a 64 bit memory addressable CPU. What are the values
of sizeof on lines 40, 48, and 55?
(d) 2 marks Assume the function performs it’s intended purpose of adding one element to the doubly linked list. If 5 elements were inserted in succession, list all the stack variables associated when inserting the 5th element (the point of insertion can be considered as lines 41, 52, or 59).
(e) 1 mark Assume the function performs it’s intended purpose of adding one element to the doubly linked list. What is the amount of heap memory allocated after 4 new elements are inserted successfully.
Delete - 6 marks
Write the entire function delete value(), include both the prototype and function body. delete value()
removes a specific value, int* v, from the doubly linked list. It will return a pointer to a node struct that was removed. If there is more than one value, then only one, any chosen one, is removed and is returned. NULL is returned if 1) there is no such element with the value v, or; 2) the doubly linked list is NULL, or 3) the value v is NULL.