linux-sysprog · beginner · ~10 min
RLIMIT_AS budgeting.
Implement int alloc_would_fail(long rlimit_as_bytes, long current_used_bytes, long alloc_bytes).
Return 1 if current_used + alloc > rlimit_as, else 0. Treat any negative
input as 1 (defensive: refuse).
RLIMIT_AS sets a hard wall on address-space size. Predicting whether a malloc(N) would succeed under that wall trains the intuition.
3 longs.
0/1.
Pure arithmetic.
int alloc_would_fail(long rlimit_as_bytes, long current_used_bytes, long alloc_bytes) { /* TODO */ (void)rlimit_as_bytes; (void)current_used_bytes; (void)alloc_bytes; return 0; }
Forgetting integer overflow on the sum.
Exact boundary (used + alloc == rlimit → OK). Negatives.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.