Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that you are coding in a language that supports two kinds ofstorage bindi

ID: 3616800 • Letter: A

Question

Assume that you are coding in a language that supports two kinds ofstorage bindings: stack dynamic and explicit heap dynamic. For the following two questions, present realistic codeexamples, i.e., your code should illustrate a situation thatcould plausibly arise in a real program. Your examples need not becomplete, i.e., you may leave out or only sketch parts that are notimportant to your point.
  1. Give a realistic code example where stack dynamic is moresuitable than explicit heap dynamic. Explain why stackdynamic is better for your example.
  2. Give a realistic code example where explicit heap dynamic ismore suitable than stack dynamic. Explain why explicit heapdynamic is better for your example.

Explanation / Answer

Stack Dynamic: Suppose if in your code, you are declaring an data structure thatis of constant size...you know the requirements of the datastructure. Then it is always better to allocate stack to the datastructure. Stack performs better than heap therefore we will use stackswhen and wherever we can. The constraint with stacks is variable size allocation is notpossible in stack E.g: int[] a =int[10]; //this has constant size Heap Dynamics: If we have to allocate memory of variable sizes.. we need toallocate heap memory to it E.g: Object instantiations, vectors in java and c++ etc e.g. code: Person p = new Person(name);                Vector vi = new Vector() These need variable sizes...therefore they fare well with usingvectors