[comp.os.msdos.programmer] TC++ pointers

evil@arcturus.uucp (Wade Guthrie) (10/17/90)

I have been trying to port some stuff that I did at someone else's 
computer (and had working) to my home machine w/Turbo C++.  After
hours of debugging, I found a gotcha.  The bit of code looked
something like:

	typedef struct MENU {...} MENU;

	main()
	{
		MENU *variable;
		...
		function(variable);
		...
	}

	void
	function(MENU *variable)
	{
		printf("variable is at %p\n",variable);
		...
	}

The problem is that I use the turbo debugger to print the value of
variable and it doesn't match the printed value by printf.  Now,
this works fine with other variables.  I really wouldn't care, but
dereferencing "variable" results in a spontaneous reboot.  

I found the same problem with a different program and recompiling
helped.  Not in this case though.  Could this be a memory model
problem (I believe that I'm using the large model everywhere), or
is there a known bug going about?

Thanks.
-- 
Wade Guthrie (evil@arcturus.UUCP)    | "He gasped in terror at what sounded
Rockwell International; Anaheim, CA  | like a man trying to gargle while
My opinions, not my employer's.      | fighting off a pack of wolves"
                                     |                Hitchhiker's Guide

joe@mdbs.uucp (Joe Greer) (10/22/90)

In article <1990Oct16.212118.6736@arcturus.uucp> evil@arcturus.uucp (Wade Guthrie) writes:
>I have been trying to port some stuff that I did at someone else's 
>computer (and had working) to my home machine w/Turbo C++.  After
>hours of debugging, I found a gotcha.  The bit of code looked
>something like:
>
>	typedef struct MENU {...} MENU;
>
>	main()
>	{
>		MENU *variable;
>		...
>		function(variable);
>		...
>	}
>
>	void
>	function(MENU *variable)
>	{
>		printf("variable is at %p\n",variable);
>		...
>	}
>
This looks like an instance of a fairly common 80x86 mistake.  Since function
wasn't declared before it was used, the compiler is going to assume that the
argument expected by function() is an int and coerce accordingly.  This 
typically causes pointers that look like 0x0000xxxx in the debugger.  As a rule
of thumb, make sure *every* function is declared before it is used.


Joe Greer			pur-ee!mdbs!joe