0

I have a concept question to ask regarding Week 5 Lecture 1 @ around 41:05.

the professor was mentioning that x[10] in the f() is actually touching bytes that x doesn't own. But how is it possible to have a x[10] to begin with? x is the pointer to an integer, an address that points to the first of 40 ints. Thus, how do you have a 11th 'slot' of an address?

Thanks.

1

It is true that x is a pointer, but it is a pointer to a block that can hold a maximum of 10 integer values. Arrays, in C, technically work the same way; an array name's decay to a pointer to the array (the block) and the bracket notation is just a syntactic sugar for some pointer arithmetic that is done underneath the hood.

So now that you know that the bracket notation is a syntactic sugar, what really happens when you do something like x[10] is something like *(x + 10).

If we assume that x stores the address 100 and that an int is 4 bytes long on the appliance, then *(x + 10) basically means *"add 10 * 4 to the value of x and grab the value that is in this new address"*. So this technically tries to get the int value that lives in a 4-byte memory block whose starting address is 140.

So far so good! Recall that an int is 4 bytes long. Now the question is, do the bytes 140, 141, 142, and 143 (i.e., the 4 bytes that store the int value that we are trying to get) really belong to the 40-byte long block that is pointed to by x, which recall stores the starting address of this block which is address 100?

Let's see, with a bit of visualization whether the 4-byte block whose starting address is 140 belongs to our 40-byte block that is pointed to by x or not

  +++++++++++++++++++++++++++++++++++++++++++++
  | x                                         |
  |   1st int  2nd int  3rd int  ...  10th int|
  |   +++++++  +++++++  +++++++       +++++++ |
  |   |     |  |     |  |     |       |     | |
  |   |     |  |     |  |     |       |     | |
  |   +++++++  +++++++  +++++++       +++++++ | 
  |   100-103  104-107  108-111  ...  136-139 |
  +++++++++++++++++++++++++++++++++++++++++++++

Now it's clearer that x[10] is out of our array's bounds!

0

First need to say that in some point of this part of the lecture David says:

So this is storing in x, the address of the first out of 40 ints

This is mistake he maid, what David meant to say was: The first out of 40 bytes. What in this case represents 10 ints.

Said that, with the x[10] = 0; notation, since the count starts in 0, the function is trying to acces to the 11th element of the array, but since only has been allocated memory to store 10 ints, the highest index the function should use is 9, x[0] trhough x[9]. Thus, the x[10] is accessing an element outside the memory allocated.

Just to clarify the index inside the brackets represents an element of the type declared in this case an int. So every index is jumping 4 bytes in this case.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .