🐧Memory Painpoints
Stack
int main() {
int x = 10;
} // x is automatically destroyed by end of this scopeHeap
Exercise
#include <iostream>
int* spawnScoreBonus() {
return new int(100);
}
int* generatePlayerHealth() {
int* health = new int(50);
delete health;
return health;
}
int main() {
int* score = spawnScoreBonus();
std::cout << "Player gained score: " << *score << std::endl;
int* health = generatePlayerHealth();
std::cout << "Player health: " << *health << std::endl;
int* enemyHp = new int(200);
int* archerTarget = enemyHp;
int* knightTarget = enemyHp;
delete knightTarget;
delete archerTarget;
return 0;
}Last updated
Was this helpful?