kinda wild ptr??

K

Kelvin@!!!

hi everyone...

i discover this accentally when i was debugging a program...
here is the problem( or maybe not :) )
*************CODE*******************
#include <iostream>
using namespace std;

int main(){
int i = 0;
int n[10] = { 0,1,2,3,4,5,6,7,8,9 };
int* ptr = &n[0];

for(i=0;i<=n[9];i++){
cout << *ptr++ << " ";
}
cout << endl;

return 0;
}
************END*********************
this program, as u can tell, perfectly does the work... it prints out all
the elements in array n...
but when the loop is finished...
ptr is pointing to the address after n[9] which is not what im expecting...

this looks really simily to the famous buffer OVERFLOW bug...
ptr is pointing to a out-of-control address...
in this little demo... im sure it does not harm to the system... and i know
how to fix this lil out-of-control ptr...
but for this particular piece of code, will it become a potential issue to
the system in other situations by any chance??

if it will, could you please list some of the situations that will trigger a
problem??
is there any guild line in C++ standard to prevend this kinda problem??

I am really appreciated for any piece of suggestion..
thank you very much...
:)
 
J

John Harrison

Kelvin@!!! said:
hi everyone...

i discover this accentally when i was debugging a program...
here is the problem( or maybe not :) )
*************CODE*******************
#include <iostream>
using namespace std;

int main(){
int i = 0;
int n[10] = { 0,1,2,3,4,5,6,7,8,9 };
int* ptr = &n[0];

for(i=0;i<=n[9];i++){
cout << *ptr++ << " ";
}
cout << endl;

return 0;
}
************END*********************
this program, as u can tell, perfectly does the work... it prints out all
the elements in array n...
but when the loop is finished...
ptr is pointing to the address after n[9] which is not what im expecting...

Why not? You go round the loop 10 times, so ptr get incremented 10 times, it
starts at the beginning of the array, so ends up pointing one past the end
of the array.

What were you expecting?
this looks really simily to the famous buffer OVERFLOW bug...
ptr is pointing to a out-of-control address...

Pointing one past the end of an array is not an illegal address, it is
explicitly allowed in the C++ standard. It illegal to dereference such a
pointer, but it is not illegal to use such a pointer (for instance to
compare it to another pointer). OTOH one before the beginning of an array is
completely illegal.
in this little demo... im sure it does not harm to the system... and i know
how to fix this lil out-of-control ptr...
but for this particular piece of code, will it become a potential issue to
the system in other situations by any chance??

if it will, could you please list some of the situations that will trigger a
problem??

Dereferencing the pointer.
is there any guild line in C++ standard to prevend this kinda problem??

Don't dereference the pointer.
I am really appreciated for any piece of suggestion..
thank you very much...
:)

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top