Question: Write code to define these five items: an int an array of ints with 1 0 0 elements a dynamically allocated int a dynamically allocated
Write code to define these five items:
an int
an array of ints with elements
a dynamically allocated int
a dynamically allocated array of ints, with elements
a set of dynamically allocated ints, using an array of pointers
Then, add to that code:
in part set the int to the value
part set the element with index of the array to
part set the dynamically allocated object to
part the element with index of the dynamically allocated array to
part set the th dynamically allocated int that would be referenced via index in
the pointer array to value
For example, here is the complete answer for :
int ;
;
Imagine that you need to define n number of dynamically allocated ints, where n is some number. To do this, you would need n int pointers. You could name the pointers p p p and so on up to pn but this gets really inconvenient as n gets larger. So instead of this, we use
a pointer array. You decide ahead of time what the maximum number of individually allocated ints is going to be say it's You define an array of int pointers. None are used initially, so they are all initialized to nullptr. As you allocate ints, you use pointers in the pointer
array. You'll need to keep track of which pointers in the array have been used so that you don't accidentally destroy one.
Here's a picture of what this looks like in memory. This particular structure has a pointer array with five elements. Three elements have been used to allocate ints, two are unused and retain their initial values of nullptr
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
