How this code is executed ?

P

pai

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
 
S

Sylvester Hesp

pai said:
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
 
D

Daniel T.

"pai said:
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;

}

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

David O

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.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top