This question has 4 parts. a) In general, when is it better to allocate an objec
ID: 3665749 • Letter: T
Question
This question has 4 parts. 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. c) 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)
stack type is better when dont want to allocate or deallocate memort manually. When we want fast
access, then stack is better.
** When we want to store numbers and remove them from back, stack is useful.
Stack stk = new Stack();
stk.push(12);
stk.push(14);
stk.pop(); --> gives 14
stk.pop(); --> gives 12
-------------------------------------------------------------------------------------------------
b) Heap allocation is dynamic allocation. When we want to allocate extra memory dynamically, or
free memory which is unwanted, heap allocation is better. In stacks, once memory allocated then
memory cannot be freed.
** When calculating complex calculations, we split the complex calculations into simpler one and
store them into each memoty. Finally we collect all data from memory and calculate further steps..
After completing calculations, we can deallocate memory. So a lot of space is saved.
=-----------------------------------------------------------------------------------------------
c)
Passing by reference or passing by pointer is not a big difference, almost they are same.
But in terms of speed, reference is fast.
By sending values by pass by reference, we can have variables changes inside function are accessible
in main block too.. We can return multiple values from functions using reference.
Eg:
void changes(int &x){
x = 5;
}
int main(){
int x = 10;
cout<<"The value of x is: "<<x;
//caling pass by reference
changes(x);
cout<<"After passing function the value of x is: "<<x;
}
------------------------------------------------------------------------------------------------------
d)
Using pointer we can pass null pointers, where using reference we cannot pass null values.
Eg:
//pass by pointer
void changes(int *x){
x = 5;
}
int main(){
int x = 10;
cout<<"The value of x is: "<<x;
//caling pass by pointer
changes(&x);
cout<<"After passing function the value of x is: "<<x;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.