-
November 26th, 2013, 12:47 PM
#1
calling mem ber function by object pointer
Hi,
I have the following piece of code.
Code:
#include<iostream>
using namespace std;
class Test
{
public:
Test(){cout<<"Test"<<endl;}
void fun()
{
int i=5;
cout<<"fun"<<i<<endl;
}
};
int main()
{
Test *ptr = NULL;
ptr->fun();
return 0;
}
compiled with g++.
Executing this give output fun5.
It is correct? I have not allocated any object and so this pointer is not created.
Then how it is able to run and call the function
-
November 26th, 2013, 12:59 PM
#2
Re: calling mem ber function by object pointer
Originally Posted by Rajesh1978
It is correct? I have not allocated any object and so this pointer is not created.
Then how it is able to run and call the function
The pointer is created: it is a null pointer. The code is obviously wrong: you're dereferencing a null pointer, hence the behaviour is undefined. As to why you did not observe any say, a crash or other indicator that something is wrong: it is probably because the object has no member variables. However, that does not make the code correct.
-
November 26th, 2013, 02:36 PM
#3
Re: calling mem ber function by object pointer
Originally Posted by Rajesh1978
It is correct? I have not allocated any object and so this pointer is not created.
Then how it is able to run and call the function
With C++, there is no guarantee how your program will behave when you have code that is faulty. C++ isn't like other languages such as C# or Java where you have a stack trace or message box saying "you made a mistake".
For example:
Code:
int main()
{
int array[100];
array[100] = 0;
}
That is clearly out of bounds of the array, but will the "array[100] = 0" be executed without crashing? Maybe it will crash, maybe it won't. The same thing with returning pointers or references to local variables -- all of these aspects of C++ lead to undefined behaviour, even if the code is syntactically correct.
Regards,
Paul McKenzie
-
November 29th, 2013, 03:09 AM
#4
Re: calling mem ber function by object pointer
-
November 29th, 2013, 06:19 PM
#5
Re: calling mem ber function by object pointer
Originally Posted by Rajesh1978
Then how it is able to run and call the function
The objects of a class don't store function code. This code is stored in one common place and is then shared by all objects. The objects store things that are specific for each object such as instance variables.
In your case ptr is used only to identify the class to which fun() belongs and that's enougth to find the function and run it. The actual pointer value of ptr need never be used since nothing that's stored in objects is accessed. So it never becomes a problem that ptr is NULL.
But say you do this,
Code:
class Test
{
public:
Test() {cout<<"Test"<<endl;}
void fun()
{
i=5;
cout<<"fun"<<i<<endl;
}
int i; // i now belongs to the objects and not to fun()
};
Now the pointer value of ptr must be used to locate the i variable in the object but since it's NULL the program crashes.
In both cases the code is wrong though. This just explains why "undefined behaviour" may look different in different cases.
Last edited by razzle; November 30th, 2013 at 12:38 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|