Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ C++ allows a programmer to create variables while the program is running. This of course is very useful in all those cases when the

C++

image text in transcribedimage text in transcribed

C++ allows a programmer to create variables while the program is running. This of course is very useful in all those cases when the exact number of variables we are going to work with is unknown. Variables created at runtime are called dynamic variables. Since dynamic variables are created in a different portion of memory (heap) and are not associated with an identifier, you need pointers to be able to find them and use them. We use the new operator to create a new dynamic variable, for example int* myptr myptr -new int; Using new is equivalent to requesting permission to use a portion of dynamic memory as big as the data type declared (in the example, an integer). When the operation is successful, new returns the address of the memory location. You must store this address in a suitable pointer (same data type) to be able to use the new variable. When we no longer need the variable, we must make its memory location available again. Failing to do so results in unused and unaccessible memory locations taking up your heap. In extreme cases, the heap gets filled with these garbage-variables and causes the program to crash. Use the operator delete to delete a dynamic variable and free the memory. delete myptr: Note that the pointer myptr is not actually deleted. It can be used again in the program. But it is very important: to use delete before storing a new address in the pointer, or it will not be possible to free the memory location (its address was lost). To practice dynamic variables, complete the exercise below (hints in the code) To practice dynamic variables, complete the exercise below (hints in the code) LAB ACTIVITY 8.14.1: Dynamic variables 0 30 main.cpp Load default template.. 1 #include 2 using namespace std; int mainO 6 intp1; 8 //Create new dynamic variable and store its address in p1 10 11 //Get an integer value from keyboard and store it in the dynamic variable p1 12 13 14 cout"Value stored in p1"

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899