Handmade Hero » Forums » Code » Casey's criteria on types
Marchin
5 posts
#17086 Casey's criteria on types
2 weeks, 4 days ago Edited by Marchin on Dec. 29, 2018, 4:36 a.m. Reason: Initial post

So i'm following the series while coding along side with it (just finished episode 015) and one thing i still don't get is Casey's criteria when choosing between a specific int (int8, int16, etc) vs using regular int. Also i don't get why he uses bool32 instead of bool8. If someone could enlighten me about this i would be very grateful.
mmozeiko
Mārtiņš Možeiko
1860 posts / 1 project
#17088 Casey's criteria on types
2 weeks, 3 days ago

Later in series he uses int less and less. So mostly Casey uses only sized int types.

As for bool32 vs bool8 - operations with 32-bit number are more often more efficient. And when storing them inside struct then bool8 type could lead to useless 3 byte padding afterwards (if next member is >=32-bit type). Thus there's really no reason to use bool8. Only advantage is if you are storing million of them in array - but then you should use u8 or similar type for specialized code.
notnullnotvoid
Miles
20 posts / 1 project
#17090 Casey's criteria on types
2 weeks, 3 days ago Edited by Miles on Dec. 29, 2018, 11:58 a.m.

My advice is to familiarize yourself with the C struct padding/alignment rules. They're easy to understand and simple to apply, and they're your ticket to knowing when a smaller data type will save space in a struct vs. when it won't.

As for bool8 vs. plain bool, I think Casey's reasoning is that boolean overdetermination (which applies to the bool type, but not to integer types) can lead the compiler to generate slightly inefficient code.