Académique Documents
Professionnel Documents
Culture Documents
nt of the index that we use in the array. Our big block is only 10 integers long
, but nothing will yell at us if we write x[20] or even x[-5]. The index doesn't
even have to be a number. It can be any arbitrary expression. In the program we
use the variable i from the for loop to index into the array. This is a very co
mmon pattern, looping from i=0 to the length of the array, and then using i as t
he index for the array. In this way you effectively loop over the entire array,
and you can either assign to each spot in the array or use it for some calculati
on.
In the first for loop, i starts at 0, and so it will assign to the 0 spot in th
e array, the value 0 times 2. Then i increments, and we assign the first spot in
the array the value 1 times 2. Then i increments again and so on up until we as
sign to position N-1 in the array the value N-1 times 2. So we've created an arr
ay with the first 10 even numbers. Maybe evens would have been a bit better name
for the variable than x, but that would have given things away. The second for
loop then just prints the values that we have already stored inside of the array
.
Let's try running the program with both types of array declarations and take a l
ook at the output of the program. As far as we can see, the program behaves the
same way for both types of declarations. Let's also take a look at what happens
if we change the first loop to not stop at N but rather say 10,000. Way beyond t
he end of the array. Oops. Maybe you've seen this before. A segmentation fault m
eans your program has crashed. You start seeing these when you touch areas of me
mory you shouldn't be touching. Here we are touching 10,000 places beyond the st
art of x, which evidently is a place in memory we shouldn't be touching. So most
of us probably wouldn't accidentally put 10,000 instead of N, but what if we d
o something more subtle like say write less than or equal to N in the for loop c
ondition as opposed to less than N. Remember that an array only has indices from
0 to N-1, which means that index N is beyond the end of the array. The program
might not crash in this case, but it's still an error. In fact, this error is so
common that it has it's own name, an off by 1 error.
That's it for the basics. So what are the major differences between the 2 types
of array declarations? One difference is where the big block of memory goes. In
the first declaration, which I'll call the bracket-array type, though this is by
no means a conventional name, it will go on the stack. Whereas in the second, w
hich I'll call the pointer-array type, it will go on the heap. This means that w
hen the function returns, the bracket array will automatically be deallocated, w
hereas as you must explicitily call free on the pointer array or else you have a
memory leak. Additionally, the bracket array isn't actually a variable. This is
important. It's just a symbol. You can think of it as a constant that the compi
ler chooses for you. This means that we can't do something like x++ with the bra
cket type, though this is perfectly valid with the pointer type.
The pointer type is a variable. For the pointer type, we have 2 separate blocks
of memory. The variable x itself is stored in the stack and is just a single poi
nter, but the big block of memory is stored on the heap. The variable x on the s
tack just stores the address of the big block of memory on the heap. One implic
ation of this is with the size of operator. If you ask for the size of the brack
et array, it will give you the size of the big block of memory, something like 4
0 bytes, but if you ask for the size of the pointer type of array, it will give
you the size of the variable x itself, which on the appliance is likely just 4
bytes. Using the pointer-array type, it is impossible to directly ask for the s
ize of the big block of memory. This isn't usually much of a restriction since w
e very rarely want the size of the big block of memory, and we can usually calc
ulate it if we need it.
Finally, the bracket array happens to provide us with a shortcut for initializin
g an array. Let's see how we could write the first 10 even integers using the sh
ortcut initilization. With the pointer array, there isn't a way to do a shortcut
like this. This is just an introduction to what you can do with arrays. They sh
ow up in almost every program you write. Hopefully you can now see a better way
of doing the student IDs example from the beginning of the video.
My name is Rob Bowden, and this is CS50.