linux-sysprog · beginner · ~10 min

Will this allocation fail under the rlimit?

RLIMIT_AS budgeting.

Challenge

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).

Why this matters

RLIMIT_AS sets a hard wall on address-space size. Predicting whether a malloc(N) would succeed under that wall trains the intuition.

Input format

3 longs.

Output format

0/1.

Constraints

Pure arithmetic.

Starter code

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; }

Common mistakes

Forgetting integer overflow on the sum.

Edge cases to handle

Exact boundary (used + alloc == rlimit → OK). Negatives.

Complexity

O(1).

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.