Re: Cannot compile with _FILE_OFFSET_BITS = 64



James Kuyper <jameskuyper@xxxxxxxxxxx> writes:
Ravishankar S wrote:
"Scott.zhou" <rwxrxrx@xxxxxxxxx> wrote in message
news:fla6k1$j8h$1@xxxxxxxxxxxxxxxx
...
25 if (lseek(fd, 4*1024*1024*1024, SEEK_SET) == -1) { // integer
overflow
No automatic conversion to 64 bit type is done by compiler. So make it
explicit by making one of the constants as 64 bit type. This makes entire
expression 64 bit.

=> 4*1024*1024*1024LL // 64 Bit constant made by LL suffix.

That's not sufficient. The above is equivalent to
((((4*1024)*1024)*1024LL). The very first multiplication is safe; but
the second one might overflow, depending upon the value of
INT_MAX. You could solve this by moving the LL to the second 1024, but
I think that's an overly subtle solution; a maintenance programmer
might not realize why the exact position of the 'LL' is critical. I'd
apply the LL suffix to all 4.

Although lseek() is non-standard (it's POSIX, not standard C), I'll
mention that its second argument is of type off_t, which is not
necessarily the same as long long.

In the interest of writing what you actually mean, it might be better
to use a cast to off_t rather than the "LL" suffix. I'd also make the
value a named constant rather than a magic number:

#define THE_OFFSET ((off_t)4*1024*1024*1024)
...
if (lseek(fd, THE_OFFSET, SEEK_SET) == -1) {
...

Or you could apply the cast to all four constants, but if it's
isolated in a #define that's probably not as important.

The need for the conversion could be avoided by using a single literal
rather than a multiplication, but 4294967296 is a bit obscure. But
you *might* consider using 0x100000000. (Personally, I prefer the
cast.)

--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Factorial
    ... You could cast a Variant variable as a Decimal type ... Variant and CDec a number into it to make it the ... they will produce an overflow error. ... if a Variant variable is assigned a value that was cast ...
    (microsoft.public.vb.general.discussion)
  • Re: cast unsigned long to long
    ... > The cast is unnecessary; ... An example of undefined behavior is the behavior on integer overflow. ...
    (comp.lang.c)
  • Re: Strange - a simple assignment statement shows error in VC++ but works in gcc !
    ... Cerberus on the way to elysium, such as a code reviewer known to ... the conversion was intended and the person who put the cast in there ... about the automatic conversion so bunged a cast in. ... If you change architecture or OS platform, ...
    (comp.lang.c)
  • Re: Cannot compile with _FILE_OFFSET_BITS = 64
    ... No automatic conversion to 64 bit type is done by compiler. ... explicit by making one of the constants as 64 bit type. ... The very first multiplication is safe; but the second one might overflow, depending upon the value of INT_MAX. ...
    (comp.lang.c)
  • Re: How to cast int to short
    ... Jon Skeet wrote: ... Note that, by default, C# checks for integer overflow when ... performing a cast in this obvious way. ...
    (microsoft.public.dotnet.languages.csharp)