Posted by James Steele on 2001-04-10
In that tutorial, you might want to mention how array bounds are done in C/C++. Every new programmer I know (*points to himself, too*) has messed up programs by unknowingly indexing an array out of bounds. Given: int array[10]; //declares an integer array with 10 elements This does NOT say that the last index in the array is 10. What it says is there are 10 TOTAL elements in the array, indexed 0 to 9--for a total of 10 numbers. Go ahead, count the numbers from 0 to 9. There are 10: 0 is the first index, and 9 is the tenth. It's a common visual misconception to think that 'array[10]' indicates the last index in the array. Not so; it indicates the total number of elements, and because of the zero-based indexing system, 'array[10 - 1]' is the last index. Next, this statement will not work correctly: for(int i = 0; i <= 10; i++) array[i].....//do some stuff with the array here See it? You crossed the border. In the array declared above, there is no index of 10! Yet you have 'i' equaling 10 at the end. There are 10 elements, but the indexes go up to-- not including--10! The proper way to index any array is: for(int i = 0; i < 10; i++) array[i].....//do some stuff with the array here Thus, go one less than the number in the '[]'. Or just make sure you always say '< that number in the []'. Does this sound over-simplified to you? Or maybe blindingly obvious? Array-bounds breaking is a little bug that can lead to big problems that are hard to track down if you don't realize it's an error. Why? Well, say you try: array[10] = 50; What are you doing? You're writing outside the array's designated memory...and possibly overwriting another piece of reserved memory! And if you're doing this with huge amounts of data (bitmaps, structures, classes, etc...), you could overwrite large amounts of memory another program was using! The tricky part is that writing to 'array[10]' will work! No compiler errors will be generated, and the computer will retrieve the data normally! But it's probably another program's memrory you're overwriting, and that's the error! And, as it was told to me: "You go past the bounds of the arrays. Possible crash and burn." It won't always lead to errors, because the memory you overwrite might just so happen to be free space...but you won't get lucky every time! It's bad coding practice to let errors like this slide! Beware of bounds overflowing. C++ won't check for it, so you have to do it! /**********************/ > As I had mentioned before, an array is like a pointer. Actually, an > array is just a pointer that has a constant value and that has already > allocated memory for itself. Therefore, the variable 'array1' POINTS TO > a memory location, as does the variable 'array2'. Unlike pointers, you > CANNOT change the memory location an array points to. Hence, > array1=array2 does not work because you are REALLY saying is: "Let > array1 point to the memory location that array2 points to." > > What is happening in the case of the 2 structures is that their VALUES > are being copied. The variable 'array1' is a pointer, but it is > dereferenced by using array notation: array1[0]. Thus, array1[0] is the > value stored at the first memory location pointed to by 'array1'. If > you want to copy one array to another, this would work: > > char array1[10], array2[10]; > int i; > > for(i = 0; i < 10; i++) > array1[i] = array2[i]; > > A more direct approach is to do something like this: > > memcpy(array1, array2, sizeof(array1)); > > I prefer the latter. Do use this with caution, however. If array1 was > declared as a (true) pointer, the sizeof(...) function would give the > size of the pointer, not the amount of memory allocated. > > I have noticed that many people are having difficulties with > pointers/arrays. Would it be beneficial if I wrote a tutorial on this > topic? If anyone has any comments about this, do write back.
Previous post | Next post | Timeline | Home