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

Dynamic Variables. Please make sure the program Compiles :) A pointer can be use

ID: 3778410 • Letter: D

Question

Dynamic Variables. Please make sure the program Compiles :)

A pointer can be used to refer to a variable. Your program can manipulate variables even if the variables have no identifiers to name them. You can use pointers refer to these nameless variables. The new operator produces a new nameless variable and returns a pointer that points to this new variable. This type of variable is called a dynamic variable. Dynamic variables are stored in a special area called the freestore of the heap. Any new dynamic variable created by a program consumes some of the memory in the freestore. If your program creates too many dynamic variables, it will consume all of the memory in the freestore. If this happens, any additional calls to new will fail If your program no longer needs a dynamic variable, the memory used by that variable can be returned to the freestore to be reused to create other dynamic variables. The delete operator eliminates a dynamic variable and returns the memory that the dynamic variable occupied to the freestore, so the memory can be reused.

Explanation / Answer

#include <iostream>
using namespace std;

int main(){
   int *p1;

   p1= new int; // Create a dynamic variable

   cout<<"Enter an integer : ";
   cin>> *p1;
   *p1 = *p1 + 7;
   cout<< "Your input + 7 = "<< *p1 << endl;

   delete p1;   // Delete the allocated space for reuse.
}