-2

I do a simulation and want to keep the memory cost down and hence using uint8_t , uint16_t for the class members. Does cpp guarantee that they will be of 8 and 16 bits ?

Is there an difference based on the architecture of the process that I should be aware of ?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
nnrales
  • 1,481
  • 1
  • 17
  • 26
  • 2
    Read: http://en.cppreference.com/w/cpp/types/integer – Richard Critten Jun 23 '16 at 18:18
  • Isn't that the point of these types? – philkark Jun 23 '16 at 18:18
  • It would be strange if uint8_t and uint16_t had a different size. http://en.cppreference.com/w/cpp/types/integer – Leon Jun 23 '16 at 18:18
  • I just wanted to ask people who knew better ? – nnrales Jun 23 '16 at 18:20
  • the manual knows better :P – 463035818_is_not_an_ai Jun 23 '16 at 18:21
  • 1
    Yes, it does. But you should be aware that class members and even stack variables could be aligned in ways that take up more memory than simply the sums of their sizes, though this is usually a speed optimization. Personally, I wouldn't worry too much about the implementation details while you're developing your application. Premature optimization is the root of all evil. – Dan Korn Jun 23 '16 at 18:22
  • @DanKorn I have greater than 1 million members and If I can't keep the memory down I can't fit it in ram. :) Could you give me some information / sources about the alignment that takes place ? I have been searching for a good explanation about how the alignment works in cpp ? – nnrales Jun 23 '16 at 18:25
  • 1
    Memory alignment is a complex subject. Though there are some ways to manage it in C++, it's actually compiler- and platform-dependent. But Google has lots of hits for "C++ memory alignment", including several SO posts. – Dan Korn Jun 23 '16 at 18:38
  • 2
    Also, I'm not sure what you mean by 1 million members, nor what exactly you're simulating, but there's probably a way to do what you need to do without keeping your entire database in RAM. – Dan Korn Jun 23 '16 at 18:39
  • @DanKorn lots of object. the class is being used to build an object in a network . Thanks for the tip , I will look more into it , it did not even occur to me to keep the objects out of RAM. But I most probably will have to because I will have to update most of the objects at each time step ! – nnrales Jun 23 '16 at 18:42
  • One million `int's will probably fit in **cache**. There's a decent chance your program will never need a byte of RAM. – MSalters Jun 23 '16 at 20:20

3 Answers3

12

Yes, they are guaranteed to be of the exact size in their names. Or not exist at all.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
5

No, those data types are not guaranteed to be 8 or 16 bits. If they exist, they are guaranteed to be 8 or 16 bits, but they are not required to exist.

In your case, use uint_least8_t and uint_least16_t. Those are required to exist, and will be the smallest type with at least that number of bits.

  • Why is your answer "*no*" then? They are guaranteed to be of the expected size (as you say so yourself)? The question is not about the existence/definitions... – Amit Jun 23 '16 at 18:22
  • @Amit If the implementation does not define them at all, it does not define them as 8 resp. 16 bits, does it? The OP wants to know if it's safe to use `uint8_t` and `uint16_t` for all implementations. It's not. –  Jun 23 '16 at 18:25
  • @hvd takes for the least tip – nnrales Jun 23 '16 at 18:38
3

These datatypes are guaranteed to have their specified size. Otherwise they would be pointless. See http://en.cppreference.com/w/cpp/types/integer for reference.

Shiro
  • 2,610
  • 2
  • 20
  • 36