Data structures assignment
CPSC 131 Homework 1 D e a d l i n e :D e a d l i n e : Wednesday, Feb-1 (MoWe sections) Thursday, Feb-2 (TuTh sections) Turn in your submission as hard copy in class.
#1 (will be graded for credit) 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 re fe re n c e re fe re n c e over a p o i n te rp o i n te r? Give an example of a programming scenario where it is certainly better to use a reference.
CPSC 131 Homework 1 D e a d l i n e :D e a d l i n e : Wednesday, Feb-1 (MoWe sections) Thursday, Feb-2 (TuTh sections) Turn in your submission as hard copy in class.
#1 (will be graded for credit) 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 re fe re n c e re fe re n c e over a p o i n te rp o i n te r? Give an example of a programming scenario where it is certainly better to use a reference.
Homework 1
Homework 1.pdf Saved to Dropbox • Jan 26, 2017, 10=20 AM
d) Now the opposite question: in general, when is it better to use a p o i n te r p o i n te r over a re fe re n c ere fe re n c e? Give an example of a programming scenario where it is certainly better to use a pointer.
#2 (solutions will be posted) Write a snippet of C++ code that does the following:
a) Create an array of 100,000 float elements that is allocated on the heap. b) Initialize each element to 0.0. c) Free the memory associated with the array.