C ++: Datatypes, which to use and when?

They told me that I should use size_tit whenever I need 32bit unsigned int, I don’t understand why, but I think it has something to do with it, if someone compiles a program on a 16 or 64 bit machine, it unsigned intwill become 16 or 64 bits, but size_twill not, but why is it not? and how can i get the bit sizes exactly what i want?

So where is the list, which data type to use and when? for example, is there an size_talternative unsigned short? or for 32bit int? etc. How can I be sure that my data types have as many bits as I chose in the first place, and I don’t have to worry about different bit sizes on other machines?

Basically, I care more about the memory used, and not about the maximum speed that I get from doubling the memory usage, because I don't have much RAM. So I want to stop worrying if everything falls apart, if my program is compiled on a machine that is not 32bit. At the moment, ive is size_talways used when I want it to be 32 bits, but for shortI do not know what to do. Someone is helping me clear my head.

On the other hand: if I need a 64-bit variable, can I use it on a 32-bit machine successfully? and what is the name of the data type (if I want it to always be 64 bits)?

+3
source share
6 answers

size_t 32-. . 64- 64- .

stdint.h . VS2008 - . ( VS2010 ).

VS2008, MS __int32, unsigned __int32 .. .

64- : 64- 32- . , . Microsoft __int64 unsigned __int64.

+1

size_t . - 4 32- 8 64- . unsigned int . unsigned int ( ).

- , , size_t .

+3

: , , size_t, container<...>::size_type

+2
+2

size_t 32-. 16- . 64- 64- .

++ C, long 32 .

int - 16 , : , 16- , 32- int 32-. , , 16- , , . , , .

, <stdint.h>, ( C99, ++ - 1998 ) , , Boost boost/cstdint.hpp.

, , int.; -)

hth.,

+2

, , , . , , - , sizeof(...).

-, , typedef ( #ifdef , , ).

- , , , , assert(sizeof(short)==2) , , .

visual-studio-2008, . Microsoft , BYTE, DWORD LARGE_INTEGER.

windef.h winnt.h .

0
source

Source: https://habr.com/ru/post/1771489/


All Articles