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...
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...