Does Uint8 or short use less memory

Oct 9, 2012 at 7:08am
Hi all, just a curious question.

If I use a short or Uint8 or Uint16 rather then just a plain int, would I use less memory, therefore be more efficient to use Uint8 with small numbers?
Oct 9, 2012 at 8:13am
Not necessarily. Take a look at stdint.h. You'll see definitions for integral types, but then you'll see what's the fastest and the smallest equivalents. You should note that it's platform dependent.
Oct 9, 2012 at 2:14pm
Of course Uint8 would use 8 bytes and Uint16 would use 16 bytes. That's why they are named so. Hence, you will definitely conserve RAM (which would hardly matter as RAMs now have capacities in GBs). But there is another problem. In shorter types, type promotion takes place, which may slow down the program.

http://www.cplusplus.com/articles/DE18T05o/
This article contains a short description of type promotion in the end. You can also google this concept.

Type promotion doesn't take place on longer types. How long? Platform dependent!
Last edited on Oct 9, 2012 at 2:18pm
Oct 9, 2012 at 3:21pm
I suspect that eklavya sharma 2 meant 8 bits and 16 bits, not 8 and 16 bytes.

There may also be alignment issues. If you declare, for instance,

1
2
3
4
5
int32 a;
uint16 b;
int32 c;
int8 d;
int32 e;


The compiler may want to align the int32s on 4-byte word boundaries. If that's the case, the compiler will insert pad bytes before (or after) b and d, and you will end up wasting the space you were trying to save.
Oct 9, 2012 at 4:16pm
You should only be concerned about memory size of basic types if you are using them in very large quantities.

Using a short instead of an int just to [maybe] save 2 bytes of memory is pointless. Even if you have an array of 10000 elements, saving 2 bytes per element is only 20K memory saved, which isn't a whole lot by modern standards.
Oct 11, 2012 at 5:42am
Thanks for your replies, much appreciated! Oh and thanks for the article eklavya, ill have an interesting read with it :)
Topic archived. No new replies allowed.