Understanding how conversions affect memory usage is important, especially in games where performance matters. Let's break this down:
When you convert a smaller type to a bigger type, you use more memory:
#include <iostream>
using namespace std;
int main() {
// Small number types
char SmallNumber{65}; // Uses 1 byte
int BiggerNumber{SmallNumber}; // Uses 4 bytes
// The computer needs extra memory to
// store the same value!
cout << "Original char: " << SmallNumber;
cout << "\nAs integer: " << BiggerNumber;
}
Original char: A
As integer: 65
Going the other way can save memory but might lose information:
#include <iostream>
using namespace std;
int main() {
// Big number type - uses 8 bytes
double BigDecimal{3.14159265359};
// Uses 4 bytes
float SmallDecimal{
static_cast<float>(BigDecimal)};
cout.precision(10);
cout << "Original: " << BigDecimal << "\n";
cout << "Converted: " << SmallDecimal;
}
Original: 3.141592654
Converted: 3.141592741
Here's what to remember about conversions and memory.
Converting to bigger types:
Converting to smaller types:
Most of the time, you should:
Answers to questions are automatically generated and may not have been reviewed.
Going into more depth on what is happening when a variable is used as a different type