kinda wild ptr??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kelvin@!!!

    kinda wild ptr??

    hi everyone...

    i discover this accentally when i was debugging a program...
    here is the problem( or maybe not :) )
    *************CO DE************* ******
    #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...
    :)
    --
    { Kelvin@!!! }


  • John Harrison

    #2
    Re: kinda wild ptr??


    "Kelvin@!!! " <kelvin604@shaw .ca.ca> wrote in message
    news:uslmc.3631 35$Pk3.86015@pd 7tw1no...[color=blue]
    > hi everyone...
    >
    > i discover this accentally when i was debugging a program...
    > here is the problem( or maybe not :) )
    > *************CO DE************* ******
    > #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[/color]
    expecting...[color=blue]
    >[/color]

    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?
    [color=blue]
    > this looks really simily to the famous buffer OVERFLOW bug...
    > ptr is pointing to a out-of-control address...[/color]

    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.
    [color=blue]
    > in this little demo... im sure it does not harm to the system... and i[/color]
    know[color=blue]
    > 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[/color]
    a[color=blue]
    > problem??[/color]

    Dereferencing the pointer.
    [color=blue]
    > is there any guild line in C++ standard to prevend this kinda problem??[/color]

    Don't dereference the pointer.
    [color=blue]
    >
    > I am really appreciated for any piece of suggestion..
    > thank you very much...
    > :)[/color]

    john


    Comment

    Working...