Lec 08 - Multi-d Array, Efficiency
Last updated
Last updated
Slides:
Why we need this part?
In C, the normal method for us to declare an array is as follows:
However, as we have seen earlier, due to "array-decay", a
will decay to a memory address (not a pointer). So, we cannot assign a
to other memory address. For example, the following code in C is illegal
So, to "solve" this problem (actually for teaching only), C allows us to have a pointer that points to an array. Note: Not just an element, but the whole array. We can do so with:
However, you still need to pay attention that the following code to define a fixed-size array are not exactly the same!
The first one defines a pointer to a fixed-size array of length 20 (Actually it allocates a space of 20 long
on the stack first, then it defines a pointer pointing to that memory location). While the second defines an array of 20 pointers, and in this case, these 20 pointers point to a long
. (But in other cases, it can point to another array too)
In a multi-dimensional array, the Array Name Decay we have seen before still applies, but now it becomes a bit trickier.
Since an array in C consists only of a contiguous region of memory that stores the elements of the array, the address of an array is the same as the address of the first of the array. The following five statements would print out exactly the same values.
From this example, by observing each pair of the new lines that have the same meaning. We can see the essence of array decay is: If we have a 2-D array matrix
, then matrix
will decay to &matrix[0]
. Similarly, matrix[0]
will decay to &matrix[0][0]
.
Add an &
operator can be considered as .
In the code below, 10 is the num_of_rows
. What we have done here is to allocate a chunk of memory with size num_of_cols * 10
once. After that, we iteratively point the remaining 9 pointers to the correct position.
To free this kind of 2-D array, just use
This existence of this part in my note is only to serve as a example for Why we cannot use free(buckets) here?
Use the following code to allocate a dynamically size 2D array, we should free as shown in the code snippet.
Note that here we can free(canvas)
because according to Line 4, it is a "heap-object".
I believe the examples in the notes for this part is pretty complete and well documented. Personally thinking, get yourself familiar with the math equation, especially review for the problem sets and examples are pretty enough.
Given two functions and , how do we determine which one has a higher rate of growth? We say that grows faster than if we can find a , such that for all and for some constant .
For instance, which one grows faster? or ? Pick , we have . Pick , we have . Pick , we have now, and we can see that for any , , so we can conclude that grows faster than .
Here I only talk about how to expand the recurrence relation. For example, we have
To reach the base case, , so , and . We have,
This term is a geometric series with a coefficient of 1 and a common ratio of 2. Its sum can be represented as follows, which is less than
When you expand the gemetric sequence, suppose the common ratio is and the first term is . Then the sum can be expressed as , where is the number of terms in this geometric sequence.
Knowing the last term in the geometric sequence, a quick way to get the number of terms in the geometric sequence is to do the logarithmic operation. e.g. Suppose the last term is , then