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

C++ questions: a) In general, when is it better to allocate an object statically

ID: 3665703 • Letter: C

Question

C++ questions:

a) In general, when is it better to allocate an object statically on the stack (as opposed to dynamically on the heap)? Give an example of a programming scenario where an object should certainly be stack-allocated.

b) Now the opposite question: in general, when is it better to allocate an object dynamically on the heap (as opposed to the statically on the stack)? Give an example of a programming scenario where an object should certainly be heap-allocated.

c) In general, when is it better to use a reference over a pointer? Give an example of a programming scenario where it is certainly better to use a reference.

d) Now the opposite question: in general, when is it better to use a pointer over a reference? Give an example of a programming scenario where it is certainly better to use a pointer.

Explanation / Answer

a)

It is better to allocate an object statically on the stack when it is required to allocate and deallocate memory manually.

An object can be allocated statically as follows:

int main()

{

       int a[10];

}

Here, the value is allocated statically or manually to a.

b)

It is better to allocate an object dynamically on the heap when it is required to allocate extra memory dynamically or free memory which is unwanted. Dynamic memory allocation can be achieved through heaps. In stacks, once the memory is allocated, it cannot be freed.

Consider the scenario where it is required to allocate the memory space for 1000 integers but only 10 integers are entered by the user. In this case, the memory space will be wasted as the space is allocated for 1000 integers.

//static allocation

int main()

{

       int a[1000];

}

//dynamic allocation

int main()

{

       int *a = new int;

}

c)

The references are used to refer indirectly to another object, thus they are the alias of another variable.

The scenarios where references are better is as follows:

int main ()

{

      int x =10;

     int &c = x;

      cout << “The value of x is:” <<x << “, The value of c is:”

            << c;

     c = 20;

      cout << “The value of x is:” <<x << “, The value of c is:”         

           << c;

}

Here, c is the reference variable of x, it can be accessed anywhere and can be used to edit the value of x.

Here, the value of x is accessed and changed through c.

d)

The null values can be allocated through pointers. There are no null references that is, references cannot be initialized to null

The scenarios where pointers are better are as follows:

int x = 10;

int y = 30;

int *c = NULL; //this is possible through pointers only.

c = &x;         //Here, c points to x.

c = &y;         //Here, c points to y.

int* a = NULL;

if(condition)

a = &c;

else

a = &b;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote