 Welcome back, in the previous segment we discussed how memory is allocated for an array and we discussed what the value of, what value the name of an array has and we also discussed what type the name of an array has. In this segment we are going to discuss how a computer interprets something like a name of index. So the first observation or the first thing that I should tell you is that square brackets are actually considered to be a binary operator by C++. So we could have written this expression as a name square bracket index. This may look, make it look more like an operator expression. Because we are familiar with putting things inside brackets, we choose to write it as a name open bracket index close bracket. But really the interpretation of C++ is sort of like this. So what exactly is the interpretation? So first of all a name and index are the operands of the operator and a name of index means the following. So it means, this expression means a variable and which variable? The variable which is stored at a name plus s times index where s is the size of a single element of the type that a name points to. So this is a little bit of complicated definition and I am going to give an example which will make it clear. And you may say that look when I am doing this, interpreting this expression or whenever I write, whenever I write in my program a name of index, does the computer actually compute something like this? Yes it does. So it will multiply the index by something and then add it to the origin, to the name. So this is not really entirely surprising because we said that C++ is going to give you a region of memory and the region is going to start at a name and then you are going to have element 0, then you are going to have element 1, you are going to have element 2 and so on. And this distance is going to be the size of each element or it is this s and therefore if I want to get to the ith element I should know how much forward I should go and how much forward should I go, I should go forward s times whatever that index is, that is it. So that is really this calculation. So you have to note that a single multiplication and addition is done no matter how large the array is. So this is why an array is often called a random access data structure. So you can get to any element of that array in essentially the same amount of time by doing just a single multiplication and addition. So here is an example. So this is our old array Q. So we defined it by writing in Q4 and we said earlier that these are the addresses used for Q0, Q1, Q2, Q3 and we said that the name Q has value 4 and its type is n star. So let us take one of the elements say Q3 and let us see how the computer or how C++ views Q3 and whether that view matches our view. So what is Q3? So we said that first of all it is a variable. So Q, this indexing operator 3 is an expression and the result of the expression is it is a variable of the type that Q points to. And what does Q point to? Q is n star, so Q points to an n. So Q3 is an n which is good because we are expecting Q3 to be an n and Q has type n star so Q3 has type n and it is stored at address Q plus s times 3 where s is the size of a single variable of the type that Q points to. In other words it is a variable of type n which is what we already said and it is stored at Q which is 1004, s which is 4 times 3, so 10116, so it is the starting address. So it is Q3. So it is indeed what we think of Q3 as what we think of as Q3. So the computer's view and our view actually do match and it is not a surprise because I told you more pictorially how the computer gets to the position in memory where Q3 is or which element of which variable in memory. So it needs to get to that position in memory and that calculation clearly should require addition and multiplication. So how a computer gets to an n address, the index is multiplied by the element size and added to the starting address to get the position in memory where the variable is stored. And whenever you write a name of index that variable which you got to by doing this address calculation is the one which is considered the result of this expression, the result of evaluating this expression. Now in light of this discussion of how a name of index works, let us try to see what index out of range means. So here is our old array Q and suppose we execute Q of 10 equals 34. So what would this do? So if we mechanically interpret as per our rule and which is how the computer interprets it. So it is the variable of type that Q points to stored at address Q plus 10 times S where S is the size of a single variable of the type that Q points to. Again this is a mouthful but we know how to interpret it now. So it is a variable of type n because Q points to int, so it is a variable of type int. It is stored at Q plus 10 times S, so Q plus 10 times 4 equal to 1044. So this is the position where Q of 10 would have been had Q actually had 10 elements but Q does not have 10 elements. So this address is somewhere beyond the region allocated for Q. So 34, if you execute this statement 34 will get stored in some strange part of the memory which has nothing to do with Q. So that is why it is bad to have indices out of range. So some other variable will get destroyed. The other way is also bad if I write X equals Q of 10, what will happen? It will pick up the value from this 1044 and so X will get some strange value, who knows what value it gets? So again it is a bad idea to have an address which is out of range and now you see what C plus plus will do in this cases, in such cases but there is a little bit more to be said. If you read or write from an improper address such as 1044, you may store data into some wrong place, you may read data from a wrong place and sometimes C plus plus or your computer hardware may say that look some addresses are protected, why are they protected? Because they may contain code and you do not want programs to be writing data into the code region of memory or the program region of memory. So if your program tries to do that, then the hardware will raise an alarm and it will cause your program to abort. So any of these things could happen and so therefore make sure that the index is in the correct range. Now some programming languages prevent index out of range by explicitly checking. So the moment you write A of i, the language will have additional code which it generates itself, you do not have to write it, which will check first whether i lies in the range 0 through size minus 1, where the size is the size of the array in question. If it does not, then it will not make that access, it will just print an error message saying that oh you made, you gave an index out of range and the program will stop. Index checking is not done in C plus plus, why? Because it takes extra work, C plus plus wants to run very fast. Of course, C plus plus does not want wrong answers but C plus plus says, the C plus plus designers believe that it is the programmer's job, you have to ensure that your index is correct and of course you can do that, I mean you have to think a little and you have to be careful but that is your job, that is what the C plus plus designers believe. So here is an example, so we have this code and let us see what it does, so when you come to the first statement of this code, this statement will cause an array queue to be allocated in memory. What happens next? The variable r is created, remember type of r is instar, so it is meant for storing addresses. When you execute this, r will get the value q, what is the value of q, it is the starting address. So what will the value of r be, it will also be the starting address. So at this point r and q will have the same value, so now what happens when you execute this, when you execute this, this expression, since r has the same value as q, this expression is as good as q of 3, so 5 will get placed in q of 3, so when you do this printing 5 will get printed and if you print r of 3, this is not, this is also going to refer to q of 3 and so again 5 will get printed. So r and q have the same value and r of 3 and q of 3 denote the same variable and therefore this will end up printing 5 in both cases. So what have we discussed? So we have discussed that a name of index is an expression with square bracket as an operator. When the index is in the range, the expression when evaluated tells what variable is meant. If the index is out of the range, then the expression does not denote a valid variable and this calculation happens fast, there is only an addition and multiplication to be done and it happens in time independent of the array length. A name of index is a varied expression if a name is a pointer. Next all these things are going to have some bearing on how you use arrays in function calls and that is what we will see in the next segment. We will take a break.