How this code is executed ?

Discussion in 'C++' started by pai, Feb 13, 2007.

  1. pai

    pai Guest

    Hi ,

    Below is the code .

    **********************
    #include<iostream>
    using namespace std;

    class A{

    public:
    int a;
    A(){ cout << "Constructor" << endl; }
    void Ab(){ //a=10; cout << "hello " << a << endl; }

    };

    int main(){

    A *a;
    a->Ab();

    return 0;

    }
    --------------------
    This answer when givin command ./a.out is as follows
    hello 1474660693

    how could the "print" method executed while I havnt created an object
    and also the vaiable b is showing garbage value while varable have not
    been allocatedmemory.

    Can any one explain me how is this happeneing.

    Thanks
    Pai
    pai, Feb 13, 2007
    #1
    1. Advertising

  2. "pai" <> wrote in message
    news:...
    > Hi ,
    >
    > Below is the code .
    >
    > **********************
    > #include<iostream>
    > using namespace std;
    >
    > class A{
    >
    > public:
    > int a;
    > A(){ cout << "Constructor" << endl; }
    > void Ab(){ //a=10; cout << "hello " << a << endl; }
    >
    > };
    >
    > int main(){
    >
    > A *a;
    > a->Ab();
    >
    > return 0;
    >
    > }
    > --------------------
    > This answer when givin command ./a.out is as follows
    > hello 1474660693
    >
    > how could the "print" method executed while I havnt created an object
    > and also the vaiable b is showing garbage value while varable have not
    > been allocatedmemory.
    >
    > Can any one explain me how is this happeneing.
    >
    > Thanks
    > Pai
    >


    You're effectively dereferencing an unitialized pointer, which yields
    undefined behaviour, so your code could just about to anything (including
    formatting your harddrive)

    - Sylvester
    Sylvester Hesp, Feb 13, 2007
    #2
    1. Advertising

  3. pai

    Daniel T. Guest

    In article <>,
    "pai" <> wrote:

    > Hi ,
    >
    > Below is the code .
    >
    > **********************
    > #include<iostream>
    > using namespace std;
    >
    > class A{
    >
    > public:
    > int a;
    > A(){ cout << "Constructor" << endl; }
    > void Ab(){ //a=10; cout << "hello " << a << endl; }
    >
    > };
    >
    > int main(){
    >
    > A *a;
    > a->Ab();
    >
    > return 0;
    >
    > }
    > --------------------
    > This answer when givin command ./a.out is as follows
    > hello 1474660693


    Actually, because of the comment "//" the above won't even compile, but
    we will pretend the "//" isn't there.

    > how could the "print" method executed while I havnt created an object
    > and also the vaiable b is showing garbage value while varable have not
    > been allocatedmemory.
    >
    > Can any one explain me how is this happeneing.


    That is called "undefined behavior" the compiler is allowed to generate
    code that does anything, including executing the method and showing
    garbage.

    What is likely happening under the covers is that upon creating the
    pointer, it has some garbage information in it, then when you call Ab(),
    the system pretends that the garbage actually points to a real type A
    object.

    Best thing to do, is to assign NULL to 'a' upon creation.

    A* a = NULL; or A* a = 0;

    Calling Ab() on it is still undefined, but many compilers will flag that
    as a runtime error.
    Daniel T., Feb 13, 2007
    #3
  4. pai

    David O Guest

    On Feb 13, 9:23 am, "pai" <> wrote: (lightly edited)
    > Hi ,
    >
    > Below is the code .
    >
    > **********************
    > #include<iostream>
    > using namespace std;
    >
    > class A{
    >
    > public:
    > int a;
    > A(){ cout << "Constructor" << endl; }
    > void Ab()
    > {
    > //a=10;
    > cout << "hello " << a << endl;
    > }
    >
    > };
    >
    > int main(){
    >
    > A *ap;
    > ap->Ab();
    >
    > return 0;
    >
    > }
    >
    > --------------------
    > This answer when givin command ./a.out is as follows
    > hello 1474660693
    >
    > how could the "print" method executed while I havnt created an object
    > and also the vaiable b is showing garbage value while varable have not
    > been allocatedmemory.
    >
    > Can any one explain me how is this happening.
    >
    > Thanks
    > Pai


    I assume you just meant to comment out the assignment to the first
    [int] 'a' and it got lost in line wrapping - as edited above along
    with a renamed second [A *] 'a' as 'ap' to clarify what is being
    talked about, I believe what happened was this...

    The fuction Ab() was effectively compiled into this:

    void C_Ab( A *this ) // Assume this is 'C'
    {
    // a=10;
    cout << "hello " << this->a << endl;
    }

    The pointer 'ap' was in a register coincidentally initialized to a
    random location on the stack, heap, or code space: quite likely, if
    the register was used as a pointer before.

    C_Ab() was called with this random address:

    C_Ab( ap ); // == ap->Ab(); with random ap.

    and cout was called with "hello " and the contents of what the 'this'
    pointer pointed to, namely 1474660693 (this time). Other times you
    probably would get a segfault or worse.

    To really mess up, you should have declared Ab() virtual, in which
    case 'ap' would have been used to access A's virtual function table
    compiled something like this:

    struct A_Vtable { (void *)Ab_Entry(A *); };

    static A_Vtable = { &C_Ab };

    (ap->A_Vtable.Ab_Entry)( ap ); // == ap->Ab(); with random ap.

    In this case instead of getting an integer from a random location and
    printing it, you get a function pointer from nowhere in particular and
    call it. At least it's not likely to fail silently!

    Best Regards,

    David O.
    David O, Feb 13, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Galina Grechka
    Replies:
    1
    Views:
    351
    el_khiyari
    Apr 14, 2005
  2. Hristo Stevic

    highlight the piece of code executed

    Hristo Stevic, Dec 7, 2003, in forum: Java
    Replies:
    0
    Views:
    333
    Hristo Stevic
    Dec 7, 2003
  3. sk
    Replies:
    1
    Views:
    429
    Karl Seguin [MVP]
    Apr 19, 2006
  4. Michal O
    Replies:
    0
    Views:
    311
    Michal O
    Sep 21, 2004
  5. Luigi
    Replies:
    2
    Views:
    507
    Eliyahu Goldin
    May 22, 2006
Loading...

Share This Page