473,471 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Expression evaluation using __int64

I need to compute a 64 bit file offset which will get passed to the _lseeki64 function.

The inputs to the offset calculation are all unsigned shorts or unsigned longs. For example:

unsigned short page_size;
unsigned long page_index;
unsigned long page_offset;
__int64 file_offset;
file_offset = (page_size * page_index) + page_offset;

I think that the above expression is subject to integer overflow, but I am not clear on how to
ensure that the intermediate values in the expression are calculated using 64 bits. The topic on
"Integral Promotions" in the VC++ Language Reference states the following:

"C++ promotions are "value-preserving." That is, the value after the promotion is guaranteed to be
the same as the value before the promotion. In value-preserving promotions, objects of shorter
integral types (such as bit fields or objects of type char) are promoted to type int if int can
represent the full range of the original type. If int cannot represent the full range of values,
then the object is promoted to type unsigned int. Although this strategy is the same as that used by
ANSI C, value-preserving conversions do not preserve the "signedness" of the object."

The above paragraph is silent on what happens if one of the values is longer than an int. If I
change the above expression to:

file_offset = (page_size * (__int64)page_index) + page_offset;

does that guarantee that the intermediate value (page_size * (__int64)page) will be evaluated using
64 bits? Are all intermediate values involving 64 bit integers also 64 bit integers?

Jeff Bean
Sep 18 '07 #1
5 2422
Jeff Bean wrote:
I need to compute a 64 bit file offset which will get passed to the
_lseeki64 function.

The inputs to the offset calculation are all unsigned shorts or
unsigned longs. For example:

unsigned short page_size;
unsigned long page_index;
unsigned long page_offset;
__int64 file_offset;
file_offset = (page_size * page_index) + page_offset;

I think that the above expression is subject to integer overflow, but
I am not clear on how to ensure that the intermediate values in the
expression are calculated using 64 bits. The topic on "Integral
Promotions" in the VC++ Language Reference states the following:

"C++ promotions are "value-preserving." That is, the value after the
promotion is guaranteed to be the same as the value before the
promotion. In value-preserving promotions, objects of shorter
integral types (such as bit fields or objects of type char) are
promoted to type int if int can represent the full range of the
original type. If int cannot represent the full range of values, then
the object is promoted to type unsigned int. Although this strategy
is the same as that used by ANSI C, value-preserving conversions do
not preserve the "signedness" of the object."

The above paragraph is silent on what happens if one of the values is
longer than an int. If I change the above expression to:

file_offset = (page_size * (__int64)page_index) + page_offset;

does that guarantee that the intermediate value (page_size *
(__int64)page) will be evaluated using 64 bits? Are all intermediate
values involving 64 bit integers also 64 bit integers?
Yes. Casting either of the multiplicands as you've shown will guarantee
that the entire expression is evaluated as 64 bit integers.

-cd
Sep 18 '07 #2
On Mon, 17 Sep 2007 23:10:33 -0700, "Carl Daniel [VC++ MVP]"
<cp*****************************@mvps.org.nospamwr ote:
>The above paragraph is silent on what happens if one of the values is
longer than an int. If I change the above expression to:

file_offset = (page_size * (__int64)page_index) + page_offset;

does that guarantee that the intermediate value (page_size *
(__int64)page) will be evaluated using 64 bits? Are all intermediate
values involving 64 bit integers also 64 bit integers?

Yes. Casting either of the multiplicands as you've shown will guarantee
that the entire expression is evaluated as 64 bit integers.
This goes without saying, but since I once had to fix dozens of occurrences
of this in some 16-bit code (with __int64 replaced by long), I'm gonna say
it anyway. :) It's really important to put the cast in the right place; for
example, this does *not* have the same effect:

(__int64) (page_size * page_index) + page_offset // WRONG!

For the multiplication to be performed at 64-bit precision, you must cast
one of the factors, as Carl said, *not* the product.

--
Doug Harrison
Visual C++ MVP
Sep 18 '07 #3
"Carl Daniel [VC++ MVP]" wrote:
>Yes. Casting either of the multiplicands as you've shown will guarantee
that the entire expression is evaluated as 64 bit integers.
Carl, Thank you for the quick answer. Do you know of some place in the MSDN library (or the C/C++
standards) where I might have discovered for myself the rules for the width of intermediate values
during expression evaluation?

Jeff Bean
Jeff Bean
CWC Software
Tel: (480) 596-9617
Fax: (480) 443-0594
Email: je**@cwcsoftware.com
Sep 18 '07 #4

"Jeff Bean" <je**@cwcsoft.comwrote in message
news:jl********************************@4ax.com...
"Carl Daniel [VC++ MVP]" wrote:
>>Yes. Casting either of the multiplicands as you've shown will guarantee
that the entire expression is evaluated as 64 bit integers.

Carl, Thank you for the quick answer. Do you know of some place in the
MSDN library (or the C/C++
standards) where I might have discovered for myself the rules for the
width of intermediate values
during expression evaluation?
In the standard, this is primarily governed by 5.9, which states that
expressions use promotions (widening, see 4.5) not conversions (narrowing,
see 4.7), but vendor-specific types such as __int64 are not mentioned.
>
Jeff Bean
Jeff Bean
CWC Software
Tel: (480) 596-9617
Fax: (480) 443-0594
Email: je**@cwcsoftware.com

Sep 18 '07 #5
On Tue, 18 Sep 2007 16:02:49 -0500, "Ben Voigt [C++ MVP]"
<rb*@nospam.nospamwrote:
>In the standard, this is primarily governed by 5.9, which states that
expressions use promotions (widening, see 4.5) not conversions (narrowing,
see 4.7)
Chapter 5, paragraph 9 talks about the "usual arithmetic conversions",
which can involve both promotion and conversion. Note that "conversions"
aren't strictly narrowing; for example, int-to-long is a conversion, not a
promotion. The term "promotion" means conversion of a (nominally) smaller
type to int, unless int can't hold all the values of the smaller type, in
which case, the conversion goes per 4.5.
but vendor-specific types such as __int64 are not mentioned.
It's inconceivable that a compiler vendor would purposely do the unexpected
here and not extend the rules that govern built-in types to types such as
__int64. MSDN doesn't mention __int64 in the obvious places in the "Visual
C++ Language Reference", but that doesn't really bother me. I've always
"just used" the type, and it's always worked as expected.

--
Doug Harrison
Visual C++ MVP
Sep 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
2
by: Jeffrey Ganping Chen | last post by:
I'm trying to build a generic runtime C# expression evaluation engine to allow the user to evaluate any C# code blocks at runtime within the application's context. For example, obj4.p4 =...
6
by: | last post by:
The following code snippet can be build in VC 6.0, but failed in VC 2003. //////////////save the following code in t.cpp #define _MT #define _WIN32_WINNT 0x0500 #include <iostream> #include...
8
by: hurry | last post by:
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"...
8
by: Brian Blais | last post by:
Hello, I have a string input from the user, and want to parse it to a number, and would like to know how to do it. I would like to be able to accept arithmetic operations, like: '5+5'...
8
by: junky_fellow | last post by:
Hi, Sorry, for asking similar questions again and again. 1) I want to know how should we reslove the ambiguities in a c expression ? Should we use precedence table as mentioned in K&R book ...
21
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
2
by: Tim Johnson | last post by:
I'm having trouble understanding this part of the expression evaluation rules ('6.5 Expressions', second item, C99 spec; the C++ wording is presumably identical): What precisely does the...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.