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

The objective of this question is to be able to describe what happens in a progr

ID: 3665690 • Letter: T

Question

The objective of this question is to be able to describe what happens in a program involving dynamic memory allocation. Be as precise as possible.

1. Analyze the following pseudocode algorithm. Describe the output (i.e., what gets displayed to the console). Also, explain in your own words what is happening in terms of memory. Include in your answers such ideas as whether a variable is stored on the stack or the heap, allocations, deallocations, references and dereferences, etc.

Algorithm doMoreStuff(X)

Pre: X :: Integer

Return: refToInt

refToInteger ptr allocate new Integer

*ptr X

return ptr

Algorithm main

Integer myInt 3

refToInteger myPtr

myPtr doMoreStuff(myInt)

print *myPtr

deallocate myPtr

2. This second algorithm is almost identical but contains a bug.

Describe what the bug is and why it is a problem. Describe what is happening in terms of memory.

Algorithm doMoreStuff(X)

Pre: X :: Integer

Return: refToInteger

refToInteger newInt

newInt &X

return newInt

Algorithm main

Integer myInt 3

refToInteger myPtr

myPtr doMoreStuff(myInt)

print *myPtr

deallocate myPtr

Explanation / Answer

1) Let me Explain line by line.

Algorithm doMoreStuff(X)

Pre: X :: Integer ---------------------------------------> Function's input: an integer X

Return: refToInt ----------------------------------------> Function's ouput: pointer to an integer.

refToInteger ptr allocate new Integer --------------------------------------> allocate space for an integer variable to an integer pointer ptr.

*ptr X -------------------------------------> put the value of integer X into the integer referenced by the pointer ptr.

return ptr        -----------------------------> return the integer pointer.

Algorithm main

Integer myInt 3

refToInteger myPtr

myPtr doMoreStuff(myInt) --------------------------> Call function

print *myPtr -------------------------------------------------> print statement prints the value "3" since we did a calll to doMoreStuff(myInt) where myInt = 3.

deallocate myPtr --------------------------------------------> Deallocate the space allocated to the integer pointer created by the algorithm.

2) This is almost similar to the above algorithm with some minor changes. Rather than allocating space for a new integer variable, this algorithm creates an integer pointer variable and makes it to refer to the input variable X. But the integer variable X is a local variable for the algorithm and will be deallocated once the function completes its execution.

Algorithm doMoreStuff(X)

Pre: X :: Integer

Return: refToInteger

refToInteger newInt

newInt &X ------------------------------------------> newInt poiter variable is made to point to X.

return newInt ---------------------------------------------> Since X is a local variable for the function, once it completes its execution newInt will contain null.

print *myPtr                   --------------------------------> Will throw an error since myPtr contains null.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote