Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this assignment, you are asked to read a program in a hypothetical metalanguage. The rules for this language are below: the keyword int declares
In this assignment, you are asked to read a program in a hypothetical metalanguage. The rules for this language are below:
the keyword int declares an integer variable.
the keyword int declares a pointertointeger variable.
the use of the operator before a pointer means access the value pointed at by the pointer
a variables scope is the pair of braces and in which it is declared.
the keyword new performs dynamic allocation. It is used with a datatype such as int short for integer
the keyword delete performs dynamic deallocation.
IF is a conditional that acts like Karels IF conditional.
ITERATE is a loop that acts like Karels ITERATE loop.
print is a function that prints the passed in value.
execution begins at the top of the main function.
Heres an example program in this metalanguage:
void main
allocates an integer
int pointerToInteger new int;
calls AddOne, passing in pointerToInteger
dynamicInteger AddOnepointerToInteger ;
prints the value of dynamicInteger
printpointerToInteger ;
deletes the dynamically allocated value
delete pointerToInteger ;
int AddOneaddTo
adds one to addTo and returns it
return addTo ;
Now, study the following program that simulates a dicerolling game.
void main
int score;
int score;
int pRoll null;
ITERATE TIMES
pRoll RollDie;
score scorepRoll;
pRoll RollDie;
score scorepRoll;
delete pRoll;
printThe last roll was pRoll;
int winner ;
IF score is greater than score THEN
winner ;
printPlayer winner wins!;
int RollDie
int pointerToRoll new int; random number between and
return pointerToRoll;
List the scopes of all variables
Variable identifierFirst line of scope Last line of scope
List all times where dynamic memory will be allocated and deallocated.
Tips:
When the same line is executed multiple times, be sure to include it each time it executes.
The keyword new is the ONLY place where dynamic allocation occurs.
The keyword delete is the ONLY place where dynamic deallocation occurs.
Line numberAllocation or deallocation?
Does this program include any dangling pointers? Please explain as thoroughly as you can.
Does this program have any memory leaks? Please explain as thoroughly as you can. The table you filled in # should be able to help you here.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started