Click here to Skip to main content
15,886,036 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionIs it ok to use uninitialized pointer? Pin
Sameerkumar Namdeo4-Aug-10 18:46
Sameerkumar Namdeo4-Aug-10 18:46 
AnswerRe: Is it ok to use uninitialized pointer? Pin
«_Superman_»4-Aug-10 19:15
professional«_Superman_»4-Aug-10 19:15 
GeneralRe: Is it ok to use uninitialized pointer? Pin
Rick York4-Aug-10 19:34
mveRick York4-Aug-10 19:34 
GeneralRe: Is it ok to use uninitialized pointer? [modified] Pin
Cedric Moonen4-Aug-10 20:48
Cedric Moonen4-Aug-10 20:48 
GeneralRe: Is it ok to use uninitialized pointer? Pin
CPallini4-Aug-10 20:57
mveCPallini4-Aug-10 20:57 
GeneralRe: Is it ok to use uninitialized pointer? Pin
Cedric Moonen4-Aug-10 21:07
Cedric Moonen4-Aug-10 21:07 
GeneralRe: Is it ok to use uninitialized pointer? Pin
CPallini4-Aug-10 21:15
mveCPallini4-Aug-10 21:15 
AnswerRe: Is it ok to use uninitialized pointer? [modified] Pin
bleedingfingers4-Aug-10 21:19
bleedingfingers4-Aug-10 21:19 
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

AnswerRe: Is it ok to use uninitialized pointer? Pin
Sameerkumar Namdeo4-Aug-10 23:17
Sameerkumar Namdeo4-Aug-10 23:17 
AnswerRe: Is it ok to use uninitialized pointer? Pin
MuraliKrishnaP5-Aug-10 9:44
MuraliKrishnaP5-Aug-10 9:44 
Questionwhy const pointer is NOT const? how to solve the problem? Pin
includeh104-Aug-10 13:12
includeh104-Aug-10 13:12 
AnswerRe: why const pointer is NOT const? how to solve the problem? PinPopular
KingsGambit4-Aug-10 17:02
KingsGambit4-Aug-10 17:02 
AnswerRe: why const pointer is NOT const? how to solve the problem? Pin
Aescleal5-Aug-10 0:02
Aescleal5-Aug-10 0:02 
QuestionHow to get physical memory size available to a process? Pin
Code-o-mat4-Aug-10 11:25
Code-o-mat4-Aug-10 11:25 
QuestionRe: How to get physical memory size available to a process? Pin
norish5-Aug-10 4:38
norish5-Aug-10 4:38 
AnswerRe: How to get physical memory size available to a process? Pin
Code-o-mat5-Aug-10 4:54
Code-o-mat5-Aug-10 4:54 
GeneralRe: How to get physical memory size available to a process? Pin
norish5-Aug-10 19:48
norish5-Aug-10 19:48 
GeneralRe: How to get physical memory size available to a process? Pin
Code-o-mat5-Aug-10 21:55
Code-o-mat5-Aug-10 21:55 
QuestionGet/Kill focus in MFC Pin
geoyar4-Aug-10 9:23
professionalgeoyar4-Aug-10 9:23 
AnswerRe: Get/Kill focus in MFC Pin
Code-o-mat4-Aug-10 11:15
Code-o-mat4-Aug-10 11:15 
GeneralRe: Get/Kill focus in MFC Pin
geoyar5-Aug-10 12:19
professionalgeoyar5-Aug-10 12:19 
GeneralRe: Get/Kill focus in MFC Pin
Code-o-mat5-Aug-10 21:52
Code-o-mat5-Aug-10 21:52 
GeneralRe: Get/Kill focus in MFC Pin
geoyar6-Aug-10 7:29
professionalgeoyar6-Aug-10 7:29 
GeneralRe: Get/Kill focus in MFC Pin
Code-o-mat6-Aug-10 8:35
Code-o-mat6-Aug-10 8:35 
GeneralRe: Get/Kill focus in MFC Pin
geoyar6-Aug-10 10:01
professionalgeoyar6-Aug-10 10:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.