output of program

R

Rahul

Hi Everyone,

I have the following program,

class A{
private:
int a;
public :
void func1();

};

void A::func1(){
cout <<"hello world";

}

int main(){
A *a1=NULL;
a1->func1();
return 0;
}

And it prints hello world. I think it is undefined behavior but i'm
assuming that the memory for the function is allocated as the compiler
parses through the class definition and hence there should be a way to
access the memory (function)... Is there any legal way without
creating an object of the class?

Thanks in advance ! ! !
 
A

asterisc

Hi Everyone,

I have the following program,

class A{
private:
int a;
public :
void func1();

};

void A::func1(){
cout <<"hello world";

}

int main(){
A *a1=NULL;
a1->func1();
return 0;

}

And it prints hello world. I think it is undefined behavior but i'm
assuming that the memory for the function is allocated as the compiler
parses through the class definition and hence there should be a way to
access the memory (function)... Is there any legal way without
creating an object of the class?

Thanks in advance ! ! !

The code should be:
int main(){
A a1;
a1.func1();
return 0;
}
 
K

Kai-Uwe Bux

Rahul said:
Hi Everyone,

I have the following program,

class A{
private:
int a;
public :
void func1();

};

void A::func1(){
cout <<"hello world";

}

int main(){
A *a1=NULL;
a1->func1();
return 0;
}

And it prints hello world. I think it is undefined behavior

Correct. Dereferencing a null pointer is undefined behavior.

but i'm
assuming that the memory for the function is allocated as the compiler
parses through the class definition and hence there should be a way to
access the memory (function)... Is there any legal way without
creating an object of the class?

That may depend on what you mean by "access the memory (function)". First,
functions in C++ are not objects. That alone makes it tricky to "access the
memory" that on real machines is associated with a function. In the
abstract machine, a function is not represented in memory.

On top of that, _calling_ a non-static member function requires an object.


Best

Kai-Uwe Bux
 
R

Reetesh Mukul

Hi Everyone,

I have the following program,

class A{
private:
int a;
public :
void func1();

};

void A::func1(){
cout <<"hello world";

}

int main(){
A *a1=NULL;
a1->func1();
return 0;

}

And it prints hello world. I think it is undefined behavior but i'm
assuming that the memory for the function is allocated as the compiler
parses through the class definition and hence there should be a way to
access the memory (function)... Is there any legal way without
creating an object of the class?

Thanks in advance ! ! !

I am not sure, but compiler passes
func1(this);

In your case this == NULL. However since func1(...) is not accessing
'this', so things worked.
I do not think such type of code will work, if you are accessing class
bound memory.

With Regards,
Reetesh Mukul
 
I

Ian Collins

Rahul said:
Hi Everyone,

I have the following program,
Make that snippet, a program compiles...
class A{
private:
int a;
public :
void func1();

};

void A::func1(){
cout <<"hello world";

}

int main(){
A *a1=NULL;
a1->func1();
return 0;
}

And it prints hello world. I think it is undefined behavior but i'm
assuming that the memory for the function is allocated as the compiler
parses through the class definition and hence there should be a way to
access the memory (function)...
It is undefined, so what happens is speculation.

The reason you "get way" with your example is func1() does not access
any data members of the class. If it did, it would attempt to
dereference a NULL this pointer.
Is there any legal way without creating an object of the class?

Yes, make func1() a static member function.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top