Jump to content

C++ data types are confusing.

Go to solution Solved by Eigenvektor,
10 minutes ago, RTXboy123 said:

So you mean that for int, the least amount of memory is 16 bits, but it can scale up to 32 bits?

For int, you know that it can hold at least -32,767 to 32,767, so it has at least 16 bit. No other promises are made. It could be 32 bit, or even 64 bit, depending on the platform your program is executing on, but you shouldn't rely on that. It does not "scale up" or change dynamically, the size is fixed, but can vary by platform (e.g. Linux vs Windows)

 

If you need to store a 32 bit value, you should use long, because you know it will always be at least 32 bit in size, no matter the underlying platform. Likewise long long will always be at least 64 bit in size.

 

If your program only ever runs on Windows and you know that int is 32 bit on that platform, while long is 64 bit, you can get away with using them that way (e.g. if you absolutely don't want to "waste" memory space). You just need to be aware that your program isn't portable, because those assumptions might fail elsewhere.

I want an answer, long = 4 bytes or 8 bytes? Because then long long, whose meaning don't know, is then 8 bytes, but all this time I was told that long holds 8 bytes, and int has 4 bytes. wHaT?

PLEASE MARK COMMENTS AS SOLUTION IF SATISFIED!!

bigger number better, makes me look cooler.

Link to comment
https://linustechtips.com/topic/1452001-c-data-types-are-confusing/
Share on other sites

Link to post
Share on other sites

The C++ standard does not define a size in byte, but rather a number range each data type must be able to hold at minimum.

 

The actual size in byte is platform dependent, have a look at these answers to the same question:

https://stackoverflow.com/a/589684

https://stackoverflow.com/a/589685

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

5 minutes ago, Eigenvektor said:

The C++ standard does not define a size in byte, but rather a number range each data type must be able to hold at minimum.

 

The actual size in byte is platform dependent, have a look at these answers to the same question:

https://stackoverflow.com/a/589684

https://stackoverflow.com/a/589685

So you mean that for int, the least amount of memory is 16 bits, but it can scale up to 32 bits?

PLEASE MARK COMMENTS AS SOLUTION IF SATISFIED!!

bigger number better, makes me look cooler.

Link to post
Share on other sites

10 minutes ago, RTXboy123 said:

So you mean that for int, the least amount of memory is 16 bits, but it can scale up to 32 bits?

For int, you know that it can hold at least -32,767 to 32,767, so it has at least 16 bit. No other promises are made. It could be 32 bit, or even 64 bit, depending on the platform your program is executing on, but you shouldn't rely on that. It does not "scale up" or change dynamically, the size is fixed, but can vary by platform (e.g. Linux vs Windows)

 

If you need to store a 32 bit value, you should use long, because you know it will always be at least 32 bit in size, no matter the underlying platform. Likewise long long will always be at least 64 bit in size.

 

If your program only ever runs on Windows and you know that int is 32 bit on that platform, while long is 64 bit, you can get away with using them that way (e.g. if you absolutely don't want to "waste" memory space). You just need to be aware that your program isn't portable, because those assumptions might fail elsewhere.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Yeah, or if your C++ compiler is compatible with C++11 or newer , you can use the fixed size types : https://en.cppreference.com/w/cpp/types/integer

 

So you then have uint8_t  , uint32_t, int64_t , uint_least16_t  etc  (unsigned and signed . fixed size or AT LEAST that size as the architecture allows)

 

Link to post
Share on other sites

7 hours ago, Eigenvektor said:

It could be 32 bit, or even 64 bit, depending on the platform your program is executing on, but you shouldn't rely on that

I agree with what you said, except I think it's important to note that it occurs at compile time.

 

So lets say you are writing a program for a specific system that is known (e.g. a 32 bit program on Windows with VS will compile with int = 4 bytes)

 

Honestly, I get the history of where it comes from and why it can't be changed now, but I really wish they had it defined as a fixed size...because it creates confusion telling people that it's not a fixed size when compiling; I bet it creates a lot of portability issues later, as people don't realize. (and the C++11 implementation to me just looks clunky having a _t).

 

Overall, if having a fixed size is really important do what mariushm said

3735928559 - Beware of the dead beef

Link to post
Share on other sites

2 hours ago, wanderingfool2 said:

Honestly, I get the history of where it comes from and why it can't be changed now, but I really wish they had it defined as a fixed size...

Oh, I agree. I can also completely understand OPs frustration with it, because I've been there myself.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

7 hours ago, wanderingfool2 said:

I agree with what you said, except I think it's important to note that it occurs at compile time.

 

So lets say you are writing a program for a specific system that is known (e.g. a 32 bit program on Windows with VS will compile with int = 4 bytes)

 

Honestly, I get the history of where it comes from and why it can't be changed now, but I really wish they had it defined as a fixed size...because it creates confusion telling people that it's not a fixed size when compiling; I bet it creates a lot of portability issues later, as people don't realize. (and the C++11 implementation to me just looks clunky having a _t).

 

Overall, if having a fixed size is really important do what mariushm said

In my projects at work and home I typedef the integer definitions of <cstdint> to U8, U16 .. U64 and S8 .. S64. Makes defining fixed size integers much easier without losing type or size information. Also had the added benefit of it makes laying out classes and structures a lot easier since you know where and how much padding is inserted for a given architecture.

 

I also extend this to float and double with F32 and F64. About the only type I don't typedef is bool since that will probably screw with generating compile time warnings and errors. Although you can technically use any int for boolean comparison, you lose the strictness of the type.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites

8 minutes ago, trag1c said:

In my projects at work and home I typedef the integer definitions of <cstdint> to U8, U16 .. U64 and S8 .. S64. Makes defining fixed size integers much easier without losing type or size information. Also had the added benefit of it makes laying out classes and structures a lot easier since you know where and how much padding is inserted for a given architecture.

Yea, I usually use the non cls compliant uint8-64 and int8-64.  In my opinion when they created the other types they should have just gone with this (it's the one MS uses).  I get that they wanted to keep within the naming...but I mean it makes it so much more intuitive when learning to just treat it without _t...no seconding guessing or wondering the different between different languages.  int32 no one would question how many bytes that is.

3735928559 - Beware of the dead beef

Link to post
Share on other sites

21 hours ago, wanderingfool2 said:

Yea, I usually use the non cls compliant uint8-64 and int8-64....

 

21 hours ago, trag1c said:

In my projects at work and home I typedef the integer definitions of <cstdint> to U8, U16 .. U64 and S8 .. S64..

 

On 8/28/2022 at 10:23 AM, mariushm said:

Yeah, or if your C++ compiler is compatible with C++11 or newer , you can use the fixed size types : https://en.cppreference.com/w/cpp/types/integer

 

If you're working on a system where the amount of CPU cycles is important, for example microcontroller interrupt routines, you might find that using a fixed data size is sometimes unwanted and can add unnecessary instructions, the severity depending on how intensively the variable is used.

 

For example, on a 32-bit Arm core, unecessarily using 8 or 16-bit variables might incur instructions for sign-extension, zero-extension or over/underflow checks. You'd then rather use types such as uint_fast8_t, where possible, in this example designating the fastest unsigned integer with at least 8 bits.

 

This is especially true for loop iterators and other such variables.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×