adding 2st animation
This commit is contained in:
@@ -78,7 +78,57 @@ similar to the examples covered in the pointers lecture except that there is no
|
|||||||
other than the pointer variable.
|
other than the pointer variable.
|
||||||
- Dynamic allocation of primitives like ints and doubles is not very interesting or significant. What’s more important is dynamic allocation of arrays and class objects.
|
- Dynamic allocation of primitives like ints and doubles is not very interesting or significant. What’s more important is dynamic allocation of arrays and class objects.
|
||||||
|
|
||||||
- play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/dynamic_memory/example1/index.html).
|
- play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/dynamic_memory/example1/index.html) to see what exactly the above code snippet does.
|
||||||
|
|
||||||
|
## 6.3 Dynamic Allocation of Arrays
|
||||||
|
- How do we allocate an array on the stack? What is the code? What memory diagram is produced by the code?
|
||||||
|
- Declaring the size of an array at compile time doesn’t offer much flexibility. Instead we can dynamically allocate an array based on data. This gets us part-way toward the behavior of the standard library vector class. Here’s an example:
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
int main() {
|
||||||
|
std::cout << "Enter the size of the array: ";
|
||||||
|
int n,i;
|
||||||
|
std::cin >> n;
|
||||||
|
double *a = new double[n];
|
||||||
|
for (i=0; i<n; ++i) { a[i] = sqrt(i); }
|
||||||
|
for (i=0; i<n; ++i) {
|
||||||
|
if ( double(int(a[i])) == a[i] )
|
||||||
|
std::cout << i << " is a perfect square " << std::endl;
|
||||||
|
}
|
||||||
|
delete [] a;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td><img src="array.png" alt="array"</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
- The expression new double[n] asks the system to dynamically allocate enough consecutive memory to hold n
|
||||||
|
double’s (usually 8n bytes).
|
||||||
|
- What’s crucially important is that n is a variable. Therefore, its value and, as a result, the size of the
|
||||||
|
array are not known until the program is executed and the the memory must be allocated dynamically.
|
||||||
|
- The address of the start of the allocated memory is assigned to the pointer variable a.
|
||||||
|
- After this, a is treated as though it is an array. For example: a[i] = sqrt( i );
|
||||||
|
In fact, the expression a[i] is exactly equivalent to the pointer arithmetic and dereferencing expression *(a+i)
|
||||||
|
which we have seen several times before.
|
||||||
|
- After we are done using the array, the line: delete [] a; releases the memory allocated for the entire
|
||||||
|
array and calls the destructor (we’ll learn about these soon!) for each slot of the array. Deleting a dynamically
|
||||||
|
allocated array without the [] is an error (but it may not cause a crash or other noticeable problem, depending
|
||||||
|
on the type stored in the array and the specific compiler implementation).
|
||||||
|
- Since the program is ending, releasing the memory is not a major concern. However, to demonstrate
|
||||||
|
that you understand memory allocation & deallocation, you should always delete dynamically allocated
|
||||||
|
memory in this course, even if the program is terminating.
|
||||||
|
- In more substantial programs it is ABSOLUTELY CRUCIAL. If we forget to release memory repeatedly
|
||||||
|
the program can be said to have a memory leak. Long-running programs with memory leaks will eventually
|
||||||
|
run out of memory and crash.
|
||||||
|
|
||||||
|
- play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/dynamic_memory/example2/index.html) to see what exactly the above code snippet does.
|
||||||
|
|
||||||
## 6.3 Exercises
|
## 6.3 Exercises
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user