Re: function returning __int32 in place of __int64
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Wed, 28 Dec 2005 12:21:46 -0600
On 28 Dec 2005 07:44:38 -0800, "hurry" <hurrynarain@xxxxxxxxx> wrote
in comp.dsp:
> hi,
>
> I am writing a c program in VC++ 6.
>
> I have 2 files with 3 functions.
>
> file-1 having two functions "a" and "c"
> file-2 having a single function "b"
>
> with function "a" as main() , "a" calls function "b" in file-2 which,
> in turn calling function "c" in file-1.
>
> function "c" and "b" are declared to return __int64. function "c"
> returns it correctly but when it is assigned, it becomes a __int32 bit
> number ( assigned to a __int64 var).
> and interestingly, when i copy and paste the function "b" from file-2
> to file 1, the program behaves as expected.
>
> the code as is given below.
>
> when "c" returns value to "b" it becomes __int32 format.
> 9007199237963776 becomes -16777216
>
>
> file-1:
>
> #include<stdio.h>
> __int64 a();
> __int64 b();
>
> void main()
"void main()" is invalid C and produces undefined behavior, even
though Visual C++ accepts it and Microsoft's illiterate help files
show it constantly. Make it legal C and use "int main()", and add a
"return 0;" at the end.
> {
> __int64 x;
> x = b();
> }
>
> __int64 c()
> {
> __int64 y;
> y = 9007199237963776;
> return y;
> }
>
> file-2:
>
> __int64 b()
> {
> __int64 z;
> z = c();
> return z;
> }
The first cause of what you are seeing is the fact that "file-2" does
not have a prototype for function c() in scope when calling that
function. Under the older version of the C standard that your
compiler more-or-less conforms to, the compiler must assume that the
function returns an int, which is 32 bits on your platform. Any
function that does not return an int must have at least a declaration
indicating the return type in scope when it is called, or you get
undefined behavior.
So the code in b() takes whatever is in the register where ints are
returned, most likely the low 32 bits of the 64 bit value that c()
returns, and converts that into a 64 bit value. The result is most
likely just the low 32 bits of the value returned by c(), possibly
sign-extended if bit 31 is set.
What you need to do is take the prototypes out of the source file and
put them in a header file, then include the header in all source files
that define or call one of the functions:
my_header.h:
__int64 a(void);
__int64 b(void);
__int64 c(void);
Then include "my_header.h" in all the source files.
You might also have another issue, namely the constant in function C.
Again, under the rules of C, an integer numeric constant in C is taken
to be an int value, if it fits in an int, or a long value. Your
constant is too large to fit in an int or a long, so it might get
truncated even before you return it. I don't know if Microsoft's
extensions will handle this or not.
It is better to write:
__int64 y = 9007199237963776i64;
....note the "i64" suffix.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- References:
- function returning __int32 in place of __int64
- From: hurry
- function returning __int32 in place of __int64
- Prev by Date: Re: Calibrating speakers
- Next by Date: Re: C values that are not known at compile time
- Previous by thread: Re: function returning __int32 in place of __int64
- Next by thread: I am searching for "Rabiner paper for voiced and unvoiced classification"
- Index(es):
Relevant Pages
|
Loading