Skip to main content

Practice ยท 2 of 3

Checked Size Arithmetic

CHALLENGE
Difficulty: advanced+25 XP

ISO C. Implement int alloc_safe(size_t count, size_t elemsz, size_t *out_bytes) โ€” compute count*elemsz for allocation; on overflow of the multiplication, or count/elemsz 0 (refuse zero-size asks), return -1 and leave *out_bytes alone; else store the product and return 0. (On this platform you may detect overflow via division: count > SIZE_MAX / elemsz.)

Back to lesson: Practice: Hardening Drills