1. Homepage
  2. Exam
  3. COMP2017/9017 System Programming - Sample Exam - Q7 Pointers

COMP2017/9017 System Programming - Sample Exam - Q7 Pointers

This question has been solved
Engage in a Conversation

Question 7 (Memory) (20 marks)

  CourseNana.COM

The following code implements an operation on a doubly linked list to add one element. CourseNana.COM

  CourseNana.COM

30 struct node { CourseNana.COM

31 struct node *next; CourseNana.COM

32 struct node *prev; CourseNana.COM

33 int* value; CourseNana.COM

34 }; CourseNana.COM

35 CourseNana.COM

36 // takes pointer to pointer to value and performs operations on value CourseNana.COM

37 void add_value(struct node ** valuep , int* v){ CourseNana.COM

38 // printf (" addnull %d v %d" CourseNana.COM

39 if(valuep == NULL ){ CourseNana.COM

40 struct node **v = malloc(sizeof **v); CourseNana.COM

41 valuep = v; CourseNana.COM

42 CourseNana.COM

43 } CourseNana.COM

44 if( (* valuep) == NULL ){ CourseNana.COM

45 // printf (" valuep null %d",*v); CourseNana.COM

46 // set new value CourseNana.COM

47 CourseNana.COM

48 struct node *val = malloc(sizeof *val); CourseNana.COM

49 *valuep = val; CourseNana.COM

50 val -> next = NULL; CourseNana.COM

51 val -> prev = NULL; CourseNana.COM

52 val -> value = v; CourseNana.COM

53 } else if( (*valuep) -> next == NULL){ CourseNana.COM

54 // printf (" valuep next null %d v %d\n" ,*(* valuep)->value ,*v); CourseNana.COM

55 struct node *val = malloc(sizeof *val); CourseNana.COM

56 (* valuep)->next = val; CourseNana.COM

57 val -> next = NULL; CourseNana.COM

58 val -> prev = *valuep; CourseNana.COM

59 val -> value = v; CourseNana.COM

60 // printf (" valuep %d v %d\n" ,*(* valuep)->value , *v); CourseNana.COM

61 // printf (" val %d v %d\n",*val ->value , *v); CourseNana.COM

62 // ent -> values = value; CourseNana.COM

63 // add_value(ent , value ); CourseNana.COM

64 CourseNana.COM

65 } else { CourseNana.COM

66 add_value( &(* valuep)->next , v); CourseNana.COM

67 } CourseNana.COM

68 CourseNana.COM

69 } CourseNana.COM

70 ... CourseNana.COM

  CourseNana.COM

Questions CourseNana.COM

(a) Does the code compile without errors (Yes / No)? 4 marks CourseNana.COM

(i) If you answered YES, what is wrong with the function add value? annotate the code with brief CourseNana.COM

comments and arrows, circles etc. CourseNana.COM

(ii) If you answered NO, what syntax needs to be corrected to make the code compile? annotate the CourseNana.COM

code with brief comments and arrows, circles etc. CourseNana.COM


CourseNana.COM


CourseNana.COM

Error message - 4 marks CourseNana.COM

Any potential syntax errors were removed, and the program was run with the test data. The following CourseNana.COM

message is shown: CourseNana.COM

  CourseNana.COM

ASAN:DEADLYSIGNAL CourseNana.COM

- ================================================================= CourseNana.COM

- ==6==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x0000004ea9e9 bp 0x7ffd530238f0 - #0 0x4ea9e8 in add_value /home/linked-data.c:53:26 CourseNana.COM

- #1 0x4ec40c in main /home/linked-data.c:589:7 CourseNana.COM

- #2 0x7f512bd6d82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f) CourseNana.COM

- #3 0x4185e8 in _start (/home/linked-data+0x4185e8) CourseNana.COM

- AddressSanitizer can not provide additional info. CourseNana.COM

- SUMMARY: AddressSanitizer: SEGV /home/linked-data.c:53:26 in add_value CourseNana.COM

- ==6==ABORTING CourseNana.COM

Describe what the error means, what offending value is present in the stack, and how to fix it. CourseNana.COM

  CourseNana.COM

Memory size - 6 marks CourseNana.COM

(b) 2 marks Describe what the operator sizeof is achieving in the context of the code on lines 40, 48, CourseNana.COM

and 55. CourseNana.COM

  CourseNana.COM

(c) 1 mark Given that this program was run on a 64 bit memory addressable CPU. What are the values CourseNana.COM

of sizeof on lines 40, 48, and 55? CourseNana.COM

  CourseNana.COM

(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). CourseNana.COM

  CourseNana.COM

(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. CourseNana.COM

  CourseNana.COM

Delete - 6 marks CourseNana.COM

Write the entire function delete value(), include both the prototype and function body. delete value() CourseNana.COM

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. CourseNana.COM


CourseNana.COM


CourseNana.COM


CourseNana.COM


CourseNana.COM


CourseNana.COM

Get the Solution to This Question

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
System Programming代写,C Language代写,Memory代写,Pointer代写,System Programming代编,C Language代编,Memory代编,Pointer代编,System Programming代考,C Language代考,Memory代考,Pointer代考,System Programminghelp,C Languagehelp,Memoryhelp,Pointerhelp,System Programming作业代写,C Language作业代写,Memory作业代写,Pointer作业代写,System Programming编程代写,C Language编程代写,Memory编程代写,Pointer编程代写,System Programmingprogramming help,C Languageprogramming help,Memoryprogramming help,Pointerprogramming help,System Programmingassignment help,C Languageassignment help,Memoryassignment help,Pointerassignment help,System Programmingsolution,C Languagesolution,Memorysolution,Pointersolution,