Is it ok to use uninitialized pointer?
-
I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?
int func_2(int i, float j)
{
cout << i << j << endl;
return i;
}class ABC
{
public:
int func_1(int i, float j)
{
return func_2(i, j);
}
};[1] ABC *pObj;
[2] int i = 10;
[3] float f = 20.50f;
[4] int r = pObj->func_1(i, f);When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:
Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.
but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.
-
I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?
int func_2(int i, float j)
{
cout << i << j << endl;
return i;
}class ABC
{
public:
int func_1(int i, float j)
{
return func_2(i, j);
}
};[1] ABC *pObj;
[2] int i = 10;
[3] float f = 20.50f;
[4] int r = pObj->func_1(i, f);When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:
Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.
but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.
Technically it can be used this way. But if such a situation arises, you should make the function a static function and call it using the class scope operator. The error is issued because these things usually tend to become bugs.
«_Superman_»
I love work. It gives me something to do between weekends. -
Technically it can be used this way. But if such a situation arises, you should make the function a static function and call it using the class scope operator. The error is issued because these things usually tend to become bugs.
«_Superman_»
I love work. It gives me something to do between weekends.I thought there must be an instance of an object to use a pointer to one. Even with a static member that is not the best syntax to use though. I hesitate to say correct because I am not a language expert. To use a static method the usual syntax is ClassName::MethodName(); when no instance is referenced.
-
I thought there must be an instance of an object to use a pointer to one. Even with a static member that is not the best syntax to use though. I hesitate to say correct because I am not a language expert. To use a static method the usual syntax is ClassName::MethodName(); when no instance is referenced.
Rick York wrote:
I thought there must be an instance of an object to use a pointer to one.
If the function which is called do not access any members of the class, then it is "safe". But of cours it is absolutely bad practice.
Rick York wrote:
Even with a static member that is not the best syntax to use though.
This, on the other hand is perfectly acceptable. I should take a coffee before answering questions on CP :) . This is not that acceptable, since the static function should accessed this way:
ClassName::MethodName()
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++modified on Thursday, August 5, 2010 3:07 AM
-
Rick York wrote:
I thought there must be an instance of an object to use a pointer to one.
If the function which is called do not access any members of the class, then it is "safe". But of cours it is absolutely bad practice.
Rick York wrote:
Even with a static member that is not the best syntax to use though.
This, on the other hand is perfectly acceptable. I should take a coffee before answering questions on CP :) . This is not that acceptable, since the static function should accessed this way:
ClassName::MethodName()
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++modified on Thursday, August 5, 2010 3:07 AM
Cedric Moonen wrote:
This, on the other hand is perfectly acceptable.
However, the wise developer would use the classname::method syntax. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Cedric Moonen wrote:
This, on the other hand is perfectly acceptable.
However, the wise developer would use the classname::method syntax. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]I think I was not awake when I replied, I really thought he was talking about accessing the static function using the classname::method syntax. Pff, ok, I'll fetch a coffee now.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
I think I was not awake when I replied, I really thought he was talking about accessing the static function using the classname::method syntax. Pff, ok, I'll fetch a coffee now.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++:) I can barely reach my workstation, without caffein. :laugh:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?
int func_2(int i, float j)
{
cout << i << j << endl;
return i;
}class ABC
{
public:
int func_1(int i, float j)
{
return func_2(i, j);
}
};[1] ABC *pObj;
[2] int i = 10;
[3] float f = 20.50f;
[4] int r = pObj->func_1(i, f);When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:
Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.
but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.
With the risk of being wordy... This piece of code should illustrate things.
class Z
{
public:
int g;
void out(){printf("addr %p\n", this);}
void outg(){printf("addr %p g=%d\n", this, g);}
};Z *pz1, *pz2=0, *pz3=(Z*)0xdeadbabe, *pz4, z;
void main()
{
pz1->out();// line 1
pz2->out();// line 2
pz3->out();// line 3
pz4->out();// line 4
z.g=100;// line 5
pz1=&z;// line 6
pz1->outg();// line 7
pz2->outg();// line 8
pz3->outg();// line 9
pz4->outg();// line 10
}As you can see, pz1 to pz4 are either not initialized or are initialized to "wrong" values. However, lines 1 to 4 execute happily, even though you dereferance using these wrong pointers. Thats because you aren't actually touching the member variable at all. Now, look at what happens next. At line 6, pz1 is initiated to a valid address and hence outg() doesn't complain. Why should it complain you may ask. I'll tell you. Open the disassembly window and step into the function call at line 8. There you'll find that the control actually steps into the function, even when derefeneced using a wrong pointer! Thats because one line of C/C++ translates into many many lines of assembler. As you step past each assembler instruction, you'll encounter this instruction
mov ecx, dword ptr [eax]
and that is where you get the tantrum. In this line, the member variable 'g' is attempted to be accessed. EAX really holds the address of the object but because 'g' is the first member, the address coincide. And the complaint is valid as you cannot CANNOT SHOULD NOT access data from uninitialized memory. EDIT: I forgot to mention that you have to turn off "incremental linking". It doesn't matter much except that if its on you might get confused and/or distracted by the " @ILT+nnn " that you'll encounter. An oh, its MSVC6.0
...byte till it megahertz...
modified on Thursday, August 5, 2010 3:33 AM
-
I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?
int func_2(int i, float j)
{
cout << i << j << endl;
return i;
}class ABC
{
public:
int func_1(int i, float j)
{
return func_2(i, j);
}
};[1] ABC *pObj;
[2] int i = 10;
[3] float f = 20.50f;
[4] int r = pObj->func_1(i, f);When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:
Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.
but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.
Thanks to all of you.
-
I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?
int func_2(int i, float j)
{
cout << i << j << endl;
return i;
}class ABC
{
public:
int func_1(int i, float j)
{
return func_2(i, j);
}
};[1] ABC *pObj;
[2] int i = 10;
[3] float f = 20.50f;
[4] int r = pObj->func_1(i, f);When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:
Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.
but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.
ccpptrain wrote:
->Whether this method of using uninitialized pointer is acceptable in any condition?
It is acceptable as long as you are careful, as you promised absolutely no data members. With this condition, you initialize or don't initialize or simply call using 0 (zero) pointer, it is acceptable.
ccpptrain wrote:
->Will this work on different platforms?
It is same for all platforms.
ccpptrain wrote:
->Any pitfalls?
One more promise: don't delete the pObj.
ccpptrain wrote:
->Your Valuable suggestions.
Don't do like this. You never know when you are going to add member variables and access them.