Hello guys,
I am relatively new to C++ programming but have good experience in PHP. Anyways, I wrote this simple program to check how pointers behave in an array but there is this one little thing I did not understand. The last index on the array is generating an overflow type value when pointer is moved to it to display the index value (it should be 0 but it isnt.)
Now i dont undertand why this is happening and how can I avoid this in future?
You are incrementing arrPtr BEFORE you're reading from it. So you're not showing index 0-9. You're showing index 1-9, and then you're reading beyond the end of the array. You're reading whatever value happens to be off the end of the array.
To add on to what @Repeater said, arrays in C++ are always indexed from 0 to n-1, not 1 to n. The idiom for array access is as follows:
1 2 3 4 5 6 7 8 9 10
// set up an array of known size
const size_t ARRAY_SIZE = 10;
int array[ARRAY_SIZE];
// access elements within the array
// always loop starting at 0, while index < size
for (int i = 0; i < ARRAY_SIZE; ++i)
{
array[i] = 0;
}
@Repeater: Thanks a lot friend. So this becomes a serious error from my side where one can manipulate to hack into it. Wow...
Anyways, using ++arrPtr is also giving off the same problem. I dont know if its buffer overlow or integer overflow because I haven't gotten to that concept clearance yet. I just read about them superficially so far.
If I increase the array size to 11 and read only 10 indices, the problem doesnt occur.
Also, at array size 11, and reading from i=0,i<=10;i++ with arrPtr++ doesnt reproduce the problem. It seems to be doing it at [10] size only :/
@doug: Thanks for the hint sir. the array size in my prog is 10 and I am reading it until loop remains<=9 so it will read from index 0 to 9 (completing all 10 indices). Putting only "<9" would end the loop at index 8. Confirmed it too by executing the given logic. Your code is array initialization in a loop with ++i (increment and read)
@doug: Thanks for the hint sir. the array size in my prog is 10 and I am reading it until loop remains<=9 so it will read from index 0 to 9 (completing all 10 indices). Putting only "<9" would end the loop at index 8. Confirmed it too by executing the given logic.
You missed the point.
Sizing an array to 10 and then looping while i <= 9 will do what you want it to do, but it's not the way C++ programmers generally do it. Probably 99% of the time in this situation C++ programmers will write code to loop while i < 10. Looping while i <= 9 looks funny to C++ programmers, and you should get used to the standard, idiomatic C++ way of writing code. In your code, that would be for (int i = 0; i < 10; i++) // or ++i, no difference in this case
If you look at the example I gave you, the size of the array and the loop limit are the same value (10 in your example). I did not create a size-10 array and loop from 0 to 8.
Your code is array initialization in a loop with ++i (increment and read)
Yes, I was initializing an array in my example. I was only demonstrating the typical C++ way to loop through an array. You can obviously do whatever you want (set / get / manipulate) the array element once you isolate it in the loop. Initialization vs. reading wasn't the point of my example.
And the difference between i++ vs. ++i is meaningless in the example. In old compilers ++i was probably slightly faster than i++ because i++ had to save the original value in case it was being assigned. Someone drilled it into me to use ++i whenever the original value was not being used, so I default to that. Modern compilers should recognize this situation and provide just-as-efficient code either way. I'm sorry if I added confusion with this.
@repeater: The //GOOD logic worked. learnt a new thing with this.. read and then increment. so this is what happened. I was incrementing arrPtr before it read and it actually started from 1 and read until 10th index which wasnt there. Now I understand your first reply about reading/incrementing. first I thought you were saying about ++* or *++. :D Thanks alot sir
@doug: Wow... I am really bad with C++ programming.. To be honest, I did learn yet a new thing. Programming etiquette of C++ and the logic about having ++* or *++ being the same thing now so I wouldnt be worrying about it in future :) Thanks to you :)