c Fundamental Programming a Consider each of the following q
c++ Fundamental Programming
 
 (a) Consider each of the following questions carefully. You are required to give the
 answer true or false and justify your answer. If it is true, explain why it is true.
 i. Linked lists are statically allocated.
 [2 marks]
 ii. Automatic variables are destroyed (deallocated) by the C++ runtime system.
 [2 marks]
 iii. You cannot make a reference to unallocated memory when using a vector.
 [2 marks]
 iv. The virtual keyword turns off runtime checking.
 [2 marks]
 v. A doubly linked list uses twice as much space as a singly linked list.
 [2 marks]
 (b) Write a loop that will search an array of int values, myArray, to determine if
 it contains the integer value stored in a variable myInt and display the message
 “Found” if it finds it.
 Clearly comment your code. You may assume the myArray and myInt are both
 declared and initialised.
 [6 marks]
Solution
i. Linked lists are statically allocated.
Answer) False
In a typical linked list, each node is dynamically allocated individually. At least if you\'re storing small data items (such as int) the memory allocated for a node may be (usually will be) even larger than the amount you actually request. So if we ask for 12 bytes of memory to hold an int and a pointer -- but the chunk of memory we get is likely to be rounded up to 16 or 32 bytes instead
ii. Automatic variables are destroyed (deallocated) by the C++ runtime system.
Answer) True
Memory allocation and deallocation happens automatically (when the variable is instantiated / destroyed) by the C++ runtime system.
Automatic memory allocation happens for function parameters and local variables. Memory for these types of variables is allocated when the relevant block is entered, and freed when the block is exited, as many times as necessary.
iii. You cannot make a reference to unallocated memory when using a vector.
Answer) True
Accessing an unallocated area of memory is Undefined Behavior, whereas it will throw an exception before any of that occurs.
iv. The virtual keyword turns off runtime checking.
Answer) True
Turn off all access checking. This switch is mainly useful for working around bugs in the access control code
v. A doubly linked list uses twice as much space as a singly linked list.
Answer) True
doubly linked list requires Two Pointers per node and so in the implemented presented it requires twice as much Overhead as a singly linked list.

