Float parameters "corrupted" - solution found !!!!!

brian_helterline brianh at hpcvia.CV.HP.COM
Wed Nov 7 03:21:43 AEST 1990


>I posted this question recently:

>> I am having a problem with Microsoft C (V 5.1).
>> 
>> When I pass a parameter of type float (by value) to a function, by the time
>> it is read from the stack by the function it is corrupted.
>> The corrupted value is something very small (eg X.XXXXe-XXX) and varies
>> depending on the parameter.

>I forgot to state in my original posting was that I declared ANSI style
>prototypes for external functions. 
>I also did not mention that this was not a repeatable "bug".
>By this I mean that each time the program was complied you could not
>guarantee that the problem would appear.

>I wish to thank everyone that replied to this question.
>The net really helped me out on this one - I was getting desperate.

>In fact many people had suffered the same problem in past.
>I always find this very re-assuring !
>It would appear that the root of the problem is that the compiler
>sometimes (unexpectedly ?) converts a floats parameter to a double.
            ^^^^^^^^^^^^^^
	    No, it always will if you use old style function definitions

>The most common solution I received was to declare the external function
>prototypes in ANSI format. 
>As discussed above this did not solve the problem.
>However all this talk of float a doubles led me to fiddle with parameter
>types. I appear to have fixed the problem with the following:

>1. Leave external function with the function float parameter alone.
>2. In the external function prototype section of the calling module, declare the
>   float parameter as a double.

>Can anyone comment and tell me why this has worked ?

	If you use the /Zg option of MSC5.1, it will generate the prototypes
	for you and use will see that it actually will prototype your
	function as having a parameter of type double rather than float.

	If you use old style function definitions (not prototypes), let
	MSC generate your external prototypes for you and you won't have
	any problems.  Else, use ANSI stype function definitions.  Here
	is a brief example:

	float foo(a,b)	/* old style declaration - really a double */
	int a;
	float b;	/* really a double */
	{
	.....
	}

	If you run this through MSC using /Zg, the prototype will come
	out as
	double foo(int a, double b);

	but, if you have

	float foo( int a, float b )	/* ANSI style declaration */
	{
	......
	}
        
	MSC will generate the prototype like:

	float foo(int a, float b);


	Hope this helps......

	-Brian

>Thanks again to all who replied,

Henry.
----------



More information about the Comp.lang.c mailing list