Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

'int' considered dangerous (reprise)

85 views
Skip to first unread message

Mr Flibble

unread,
Jan 27, 2016, 3:10:12 PM1/27/16
to
'int' considered dangerous: use a <cstdint> sized integer typedef instead.

On a related matter eschewing unsigned integer types is foolish as one
should use an unsigned type when dealing with unsigned values.

On another related matter the return type of std::uncaught_exceptions is
incorrect: it should probably be std::size_t.

/Flibble

P.S. Sausages.

Christopher Pisz

unread,
Jan 27, 2016, 3:18:06 PM1/27/16
to
I guess your weekly, "Let's post the same fucking thing again and see if
people will argue about it yet again" alarm has gone off.



Mr Flibble

unread,
Jan 27, 2016, 3:27:32 PM1/27/16
to
I thought I was in your killfile mate? Judging by the quality of your
other posts in this NG recently it would probably benefit a lot of
people if you re-added me to it sausages.

/Flibble

Gareth Owen

unread,
Jan 27, 2016, 4:39:39 PM1/27/16
to
Mr Flibble <flibbleREM...@i42.co.uk> writes:

> On a related matter eschewing unsigned integer types is foolish as one
> should use an unsigned type when dealing with unsigned values.

Two simple rules:
i) use an unsigned type when dealing with unsigned values
ii) there are (almost) no unsigned values
iii) sausages

Mr Flibble

unread,
Jan 27, 2016, 4:50:19 PM1/27/16
to
ii) and iii) are in conflict as you cannot eat a negative number of
sausages.

/Flibble

Rosario19

unread,
Jan 28, 2016, 12:26:00 AM1/28/16
to
use unsigned type of fixed size
because
1) assembly programmers use unsigned fixed size registers
2) they would have 0 UBs
3) are easier to handle and write ok
4) because i always like unsigned until i see them
and not use int, and i not find hard problems with them

Öö Tiib

unread,
Jan 28, 2016, 2:58:27 AM1/28/16
to
The 'argc' of 'main' can also never be negative but that does not make
it unsigned value. It is signed value whose sign is always positive.

leigh.v....@googlemail.com

unread,
Jan 28, 2016, 4:10:41 AM1/28/16
to
argc is an artifact of ancient lazier times: if we were to reinvent main today we would do it differently.

leigh.v....@googlemail.com

unread,
Jan 28, 2016, 4:10:42 AM1/28/16
to

leigh.v....@googlemail.com

unread,
Jan 28, 2016, 4:10:42 AM1/28/16
to

Ian Collins

unread,
Jan 28, 2016, 4:17:55 AM1/28/16
to
leigh.v....@googlemail.com wrote:
> argc is an artifact of ancient lazier times: if we were to reinvent
> main today we would do it differently.

Three times differently :)

Too many sausages?

--
Ian Collins

leigh.v....@googlemail.com

unread,
Jan 28, 2016, 5:42:10 AM1/28/16
to
No, it's bloody Google Groups and Google Chrome...

/Leigh

Bo Persson

unread,
Jan 28, 2016, 8:24:36 AM1/28/16
to
What about

unsigned yesterdays_sausages = 6;
unsigned todays_sausages = 5;

unsigned difference = todays_sausages - yesterdays_sausages;


Doesn't really work, does it?



Bo Persson

Scott Lurndal

unread,
Jan 28, 2016, 8:57:51 AM1/28/16
to
I must disagree with (ii). Most of what I do[*] operates on
unsigned values.

[*] Operating systems, Hypervisors, SoC simulation - all in C++.

Juha Nieminen

unread,
Feb 1, 2016, 4:20:28 AM2/1/16
to
Mr Flibble <flibbleREM...@i42.co.uk> wrote:
> 'int' considered dangerous: use a <cstdint> sized integer typedef instead.

It depends on what you are doing. If you are writing a library that should
be as portable as possible, and expected to be used in wildly different
platforms, from ols small embedded systems to modern 64-bit processors, then
perhaps.

If, however, you are writing closed-source software for a specific platform
(let's say, for example, iOS), then it might even be better to use int for
integers whose maximum size is irrelevant. On that particular platform you
can be sure that int will have a minimum size and will never get smaller
even in future versions of that platform, so you'll be A-ok.

Why use int instead of an equivalent std type? Because 'int' is ostensibly
always the most efficient integral type of the system, even in future
versions of the platform. You'll never know if the std type you chose
will be the most efficient in a future version.

> On a related matter eschewing unsigned integer types is foolish as one
> should use an unsigned type when dealing with unsigned values.

unsigned causes problems when it's mixed with signed types. And this
happens quite easily.

Example: Image size cannot be negative. Negative values are nonsensical.
Therefore it makes sense to use unsigned to store image dimensions?
In theory yes. In practice you'd better use a signed type because
image coordinates *can* become negative in many situations (eg. line
drawing, or sprite coordinates), and you'll run into problems when
mixing signed coordinates with unsigned image sizes.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Wouter van Ooijen

unread,
Feb 1, 2016, 12:07:38 PM2/1/16
to
Op 01-Feb-16 om 10:20 AM schreef Juha Nieminen:
> Mr Flibble <flibbleREM...@i42.co.uk> wrote:
>> 'int' considered dangerous: use a <cstdint> sized integer typedef instead.
>
> It depends on what you are doing. If you are writing a library that should
> be as portable as possible, and expected to be used in wildly different
> platforms, from ols small embedded systems to modern 64-bit processors, then
> perhaps.
>
> If, however, you are writing closed-source software for a specific platform
> (let's say, for example, iOS), then it might even be better to use int for
> integers whose maximum size is irrelevant. On that particular platform you
> can be sure that int will have a minimum size and will never get smaller
> even in future versions of that platform, so you'll be A-ok.
>
> Why use int instead of an equivalent std type? Because 'int' is ostensibly
> always the most efficient integral type of the system, even in future
> versions of the platform. You'll never know if the std type you chose
> will be the most efficient in a future version.

That's why you should use the intN_t types only for layout purposes or
to save memory. For general work, use the int_fastN_t types: the fast
choice on your platform that holds at least N bits.

An additional advantage over using plain int (or short, or whatever) is
that you make clear to your reader how many bits you need, even when the
erader does not have the context knowledge of the particulr system you
wrote this code for.

Wouter van Ooijen

Mr Flibble

unread,
Feb 1, 2016, 12:45:44 PM2/1/16
to
On 01/02/2016 09:20, Juha Nieminen wrote:
> Mr Flibble <flibbleREM...@i42.co.uk> wrote:
>> 'int' considered dangerous: use a <cstdint> sized integer typedef instead.
> If, however, you are writing closed-source software for a specific platform
> (let's say, for example, iOS), then it might even be better to use int for
> integers whose maximum size is irrelevant. On that particular platform you
> can be sure that int will have a minimum size and will never get smaller
> even in future versions of that platform, so you'll be A-ok.

You'll want 'int_fast32_t' instead of 'int' then mate.

/Flibble

0 new messages