Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any advantage of using non-fixed integers (int, long) instead of fixed-size ones (int64_t, int32_t)?

Maybe performance? I feel that using non-fixed integers just makes programs more complicated and prone to fail when porting to another architecture.

like image 579
lamefun Avatar asked Dec 22 '12 15:12

lamefun


People also ask

When should you use a long instead of int as your variable type?

In Java, the type long is 64-bit signed integer type. The type long is used where the type int is not that large to hold the desired value. The range of long is –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 which is quite large, to hold the larger values like big whole numbers.

Should I use long or int?

An int is a 32-bit integer; a long is a 64-bit integer. Which one to use depends on how large the numbers are that you expect to work with. int and long are primitive types, while Integer and Long are objects.

Why are int and long same size?

Compiler designers tend to to maximize the performance of int arithmetic, making it the natural size for the underlying processor or OS, and setting up the other types accordingly. But the use of long int , since int can be omitted, it's just the same as long by definition.

Is short better than int?

Using short can conserve memory if it is narrower than int , which can be important when using a large array. Your program will use more memory in a 32-bit int system compared to a 16-bit int system.


1 Answers

std::intN_t are provided only if the implementation can directly support them. So porting code that uses them can fail.

I would prefer std::intfastN_t for general use because they have less restrictions and should be as fast or faster as int.

Also, most C++ code uses int everywhere so you might run into promotion weirdness when passing a std::int32_t into a function accepting an int, especially if sizeof(int) is only 16 bits.

like image 50
Pubby Avatar answered Oct 13 '22 10:10

Pubby