Answered step by step
Verified Expert Solution
Link Copied!

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 pointer-to-integer 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(0);
// calls AddOne, passing in pointerToInteger
dynamicInteger = AddOne(pointerToInteger );
// prints the value of dynamicInteger (1)
print(*pointerToInteger );
// deletes the dynamically allocated value
delete pointerToInteger ;
}
int AddOne(addTo)
{
// adds one to addTo and returns it
return addTo +1;
}
Now, study the following program that simulates a dice-rolling game.
void main()
{
int score1=0;
int score2=0;
int* pRoll = null;
ITERATE 3 TIMES
{
pRoll = RollDie();
score1= score1+*pRoll;
pRoll = RollDie();
score2= score2+*pRoll;
delete pRoll;
}
print(The last roll was +*pRoll);
int winner =1;
IF (score2 is greater than score1) THEN
{
winner =2;
}
print(Player + winner + wins!);
}
int* RollDie()
{
int* pointerToRoll = new int(1,6); // random number between 1 and 6
return pointerToRoll;
}
1. List the scopes of all variables
Variable identifier//First line of scope //Last line of scope
2. 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 number//Allocation or deallocation?
3. Does this program include any dangling pointers? Please explain as thoroughly as you can.
4. Does this program have any memory leaks? Please explain as thoroughly as you can. The table you filled in #2 should be able to help you here.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions