function returning __int32 in place of __int64
CASTalk.com Forum Index CASTalk.com
Discussion of DSP, FPGA, storage and embedded system.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web castalk.com
function returning __int32 in place of __int64

 
Post new topic   Reply to topic    CASTalk.com Forum Index -> DSP
Author Message
hurry
Guest





Posted: Wed Dec 28, 2005 5:16 pm    Post subject: function returning __int32 in place of __int64 Reply with quote

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()
{
__int64 x;
x = b();
}

__int64 c()
{
__int64 y;
y = 9007199237963776;
return y;
}

file-2:

__int64 b()
{
__int64 z;
z = c();
return z;
}
Back to top
Bhaskar Thiagarajan
Guest





Posted: Wed Dec 28, 2005 11:32 pm    Post subject: Re: function returning __int32 in place of __int64 Reply with quote

You should be posting this in comp.lang.c

"hurry" <hurrynarain@gmail.com> wrote in message
news:1135784678.441524.38200@o13g2000cwo.googlegroups.com...
Quote:
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()
{
__int64 x;
x = b();
}

__int64 c()
{
__int64 y;
y = 9007199237963776;
return y;
}

file-2:

__int64 b()
{
__int64 z;
z = c();
return z;
}
Back to top
Jack Klein
Guest





Posted: Thu Dec 29, 2005 12:21 am    Post subject: Re: function returning __int32 in place of __int64 Reply with quote

On 28 Dec 2005 07:44:38 -0800, "hurry" <hurrynarain@gmail.com> wrote
in comp.dsp:

Quote:
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.

Quote:
{
__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
Back to top
Erik de Castro Lopo
Guest





Posted: Thu Dec 29, 2005 9:15 am    Post subject: Re: function returning __int32 in place of __int64 Reply with quote

Bhaskar Thiagarajan wrote:
Quote:

You should be posting this in comp.lang.c

Sorry, thats really bad advice. comp.lang.c is strictly
about ISO/ANSI standard C.

Quote:

"hurry" <hurrynarain@gmail.com> wrote in message
news:1135784678.441524.38200@o13g2000cwo.googlegroups.com...
hi,

I am writing a c program in VC++ 6.

This guy is actually writing in a C++ variant.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo
+-----------------------------------------------------------+
"There is no reason why anyone would want a computer in their home"
Ken Olson, DEC, 1977
Back to top
 
Post new topic   Reply to topic    CASTalk.com Forum Index -> DSP All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




VoIP Electronics Powered by phpBB