Is it a good practice to use smaller data types for variables to save memory?
- by ThePlan
When I learned the C++ language for the first time I learned that besides int, float etc, smaller or bigger versions of these data types existed within the language. For example I could call a variable x
int x;
or
short int x;
The main difference being that short int takes 2 bytes of memory while int takes 4 bytes, and short int has a lesser value, but we could also call this to make it even smaller:
int x;
short int x;
unsigned short int x;
which is even more restrictive.
My question here is if it's a good practice to use separate data types according to what values your variable take within the program. Is it a good idea to always declare variables according to these data types?