public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* shift-count-overflow possible issue on 64bit type
@ 2019-11-23 13:56 Jonny Grant
  2019-11-23 14:16 ` Ponnuvel Palaniyappan
  2019-11-25  9:30 ` Jonathan Wakely
  0 siblings, 2 replies; 8+ messages in thread
From: Jonny Grant @ 2019-11-23 13:56 UTC (permalink / raw)
  To: gcc-help

I see on my 64bit Ubuntu PC that size_t is 8 bytes, that is 64bits, 
however I see a warning:

$ g++-8 -Wall -Wextra -o size_t size_t.cpp
size_t.cpp: In function ‘int main()’:
size_t.cpp:11:21: warning: left shift count >= width of type 
[-Wshift-count-overflow]
      size_t big = 1<<40;
                      ^~



Same on Godbolt trunk.
Is this an issue?

Regards, Jonny

https://godbolt.org/z/xeefQ8

#include <stdio.h>
size_t big = 1<<41;



Please keep my email on any replies
Jonny

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shift-count-overflow possible issue on 64bit type
  2019-11-23 13:56 shift-count-overflow possible issue on 64bit type Jonny Grant
@ 2019-11-23 14:16 ` Ponnuvel Palaniyappan
  2019-11-24  1:15   ` Jonny Grant
  2019-11-25  9:30 ` Jonathan Wakely
  1 sibling, 1 reply; 8+ messages in thread
From: Ponnuvel Palaniyappan @ 2019-11-23 14:16 UTC (permalink / raw)
  To: Jonny Grant; +Cc: gcc-help

The shift actually happens as int because the type of 1 is int, and the
result is then assigned to size_t. You can instead use a wider type:

size_t big = 1UL<<41;

On Sat, 23 Nov 2019, 13:56 Jonny Grant, <jg@jguk.org> wrote:

> I see on my 64bit Ubuntu PC that size_t is 8 bytes, that is 64bits,
> however I see a warning:
>
> $ g++-8 -Wall -Wextra -o size_t size_t.cpp
> size_t.cpp: In function ‘int main()’:
> size_t.cpp:11:21: warning: left shift count >= width of type
> [-Wshift-count-overflow]
>       size_t big = 1<<40;
>                       ^~
>
>
>
> Same on Godbolt trunk.
> Is this an issue?
>
> Regards, Jonny
>
> https://godbolt.org/z/xeefQ8
>
> #include <stdio.h>
> size_t big = 1<<41;
>
>
>
> Please keep my email on any replies
> Jonny
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shift-count-overflow possible issue on 64bit type
  2019-11-23 14:16 ` Ponnuvel Palaniyappan
@ 2019-11-24  1:15   ` Jonny Grant
  0 siblings, 0 replies; 8+ messages in thread
From: Jonny Grant @ 2019-11-24  1:15 UTC (permalink / raw)
  To: Ponnuvel Palaniyappan; +Cc: gcc-help

Many thanks

I've filed a ticket, hope the warning can be enhanced.


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92642

Jonny


On 23/11/2019 14:15, Ponnuvel Palaniyappan wrote:
> The shift actually happens as int because the type of 1 is int, and 
> the result is then assigned to size_t. You can instead use a wider type:
>
> size_t big = 1UL<<41;
>
> On Sat, 23 Nov 2019, 13:56 Jonny Grant, <jg@jguk.org 
> <mailto:jg@jguk.org>> wrote:
>
>     I see on my 64bit Ubuntu PC that size_t is 8 bytes, that is 64bits,
>     however I see a warning:
>
>     $ g++-8 -Wall -Wextra -o size_t size_t.cpp
>     size_t.cpp: In function ‘int main()’:
>     size_t.cpp:11:21: warning: left shift count >= width of type
>     [-Wshift-count-overflow]
>           size_t big = 1<<40;
>                           ^~
>
>
>
>     Same on Godbolt trunk.
>     Is this an issue?
>
>     Regards, Jonny
>
>     https://godbolt.org/z/xeefQ8
>
>     #include <stdio.h>
>     size_t big = 1<<41;
>
>
>
>     Please keep my email on any replies
>     Jonny
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shift-count-overflow possible issue on 64bit type
  2019-11-23 13:56 shift-count-overflow possible issue on 64bit type Jonny Grant
  2019-11-23 14:16 ` Ponnuvel Palaniyappan
@ 2019-11-25  9:30 ` Jonathan Wakely
  2019-11-25 12:02   ` Cristiano Di Buduo
  2019-11-25 12:18   ` Florian Weimer
  1 sibling, 2 replies; 8+ messages in thread
From: Jonathan Wakely @ 2019-11-25  9:30 UTC (permalink / raw)
  To: Jonny Grant; +Cc: gcc-help

On Sat, 23 Nov 2019 at 13:56, Jonny Grant <jg@jguk.org> wrote:
>
> I see on my 64bit Ubuntu PC that size_t is 8 bytes, that is 64bits,
> however I see a warning:
>
> $ g++-8 -Wall -Wextra -o size_t size_t.cpp
> size_t.cpp: In function ‘int main()’:
> size_t.cpp:11:21: warning: left shift count >= width of type
> [-Wshift-count-overflow]
>       size_t big = 1<<40;
>                       ^~
>
>
>
> Same on Godbolt trunk.
> Is this an issue?

Why do you expect a different result for these?

A small = 1 << 40;
B big = 1 << 40;

In both cases the expression is 1 << 40, the type of A or B is irrelevant.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shift-count-overflow possible issue on 64bit type
  2019-11-25  9:30 ` Jonathan Wakely
@ 2019-11-25 12:02   ` Cristiano Di Buduo
  2019-11-26 12:41     ` Jonny Grant
  2019-11-25 12:18   ` Florian Weimer
  1 sibling, 1 reply; 8+ messages in thread
From: Cristiano Di Buduo @ 2019-11-25 12:02 UTC (permalink / raw)
  To: Jonathan Wakely, Jonny Grant; +Cc: gcc-help

> On Sat, 23 Nov 2019 at 13:56, Jonny Grant <jg@jguk.org> wrote:
> > I see on my 64bit Ubuntu PC that size_t is 8 bytes, that is 64bits,however I see a warning:
> > $ g++-8 -Wall -Wextra -o size_t size_t.cppsize_t.cpp: In function ‘int main()’:size_t.cpp:11:21:
> > warning: left shift count >= width of type[-Wshift-count-overflow]      size_t big =
> > 1<<40;                      ^~
> > 
> > 
> > Same on Godbolt trunk.Is this an issue?
> 
> Why do you expect a different result for these?
> A small = 1 << 40;B big = 1 << 40;
> In both cases the expression is 1 << 40, the type of A or B is irrelevant.

And the default type of a numeric constant is always int. (32 bits).
To make the warning go away, typecast the '1' to a size_t first.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shift-count-overflow possible issue on 64bit type
  2019-11-25  9:30 ` Jonathan Wakely
  2019-11-25 12:02   ` Cristiano Di Buduo
@ 2019-11-25 12:18   ` Florian Weimer
  2019-11-25 12:22     ` Jonathan Wakely
  1 sibling, 1 reply; 8+ messages in thread
From: Florian Weimer @ 2019-11-25 12:18 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: Jonny Grant, gcc-help

* Jonathan Wakely:

> Why do you expect a different result for these?
>
> A small = 1 << 40;
> B big = 1 << 40;
>
> In both cases the expression is 1 << 40, the type of A or B is irrelevant.

Some languages define integer literals in such a way that they have
types which do not exist at run time and evaluate constant expressions
with infinite precision.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shift-count-overflow possible issue on 64bit type
  2019-11-25 12:18   ` Florian Weimer
@ 2019-11-25 12:22     ` Jonathan Wakely
  0 siblings, 0 replies; 8+ messages in thread
From: Jonathan Wakely @ 2019-11-25 12:22 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Jonny Grant, gcc-help

On Mon, 25 Nov 2019 at 12:18, Florian Weimer wrote:
>
> * Jonathan Wakely:
>
> > Why do you expect a different result for these?
> >
> > A small = 1 << 40;
> > B big = 1 << 40;
> >
> > In both cases the expression is 1 << 40, the type of A or B is irrelevant.
>
> Some languages define integer literals in such a way that they have
> types which do not exist at run time and evaluate constant expressions
> with infinite precision.

Yes, but C and C++ do not do that. Understanding the language you're
programming in is important.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shift-count-overflow possible issue on 64bit type
  2019-11-25 12:02   ` Cristiano Di Buduo
@ 2019-11-26 12:41     ` Jonny Grant
  0 siblings, 0 replies; 8+ messages in thread
From: Jonny Grant @ 2019-11-26 12:41 UTC (permalink / raw)
  To: Cristiano Di Buduo, Jonathan Wakely; +Cc: gcc-help



On 25/11/2019 12:02, Cristiano Di Buduo wrote:
>>
>> On Sat, 23 Nov 2019 at 13:56, Jonny Grant <
>> jg@jguk.org
>>  <mailto:jg@jguk.org>
>> > wrote:
>>>
>>> I see on my 64bit Ubuntu PC that size_t is 8 bytes, that is 64bits,
>>> however I see a warning:
>>>
>>> $ g++-8 -Wall -Wextra -o size_t size_t.cpp
>>> size_t.cpp: In function ‘int main()’:
>>> size_t.cpp:11:21: warning: left shift count >= width of type
>>> [-Wshift-count-overflow]
>>>        size_t big = 1<<40;
>>>                        ^~
>>>
>>>
>>>
>>> Same on Godbolt trunk.
>>> Is this an issue?
>>
>> Why do you expect a different result for these?
>>
>> A small = 1 << 40;
>> B big = 1 << 40;
>>
>> In both cases the expression is 1 << 40, the type of A or B is irrelevant.
> 
> And the default type of a numeric constant is always int. (32 bits).
> To make the warning go away, typecast the '1' to a size_t first.

Thank you for your replies. I mistakenly expected it to use the size_t type.

I filed a ticket, for GCC to make a suggestion that was appropriate ie. 1UL
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92659

And some other related that I thought of:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92660
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92642

Jonny

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-11-26 12:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-23 13:56 shift-count-overflow possible issue on 64bit type Jonny Grant
2019-11-23 14:16 ` Ponnuvel Palaniyappan
2019-11-24  1:15   ` Jonny Grant
2019-11-25  9:30 ` Jonathan Wakely
2019-11-25 12:02   ` Cristiano Di Buduo
2019-11-26 12:41     ` Jonny Grant
2019-11-25 12:18   ` Florian Weimer
2019-11-25 12:22     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).