Interview Question

Stack vs Heap?

In portable C, distinguish automatic storage duration from dynamically allocated storage.

💡 Concept ✅ Quick Revision ⚙ C

Answer

“Stack” and “heap” are common implementation terms, not storage areas required by the C standard. • Automatic objects usually live until their block exits and are commonly implemented with a call stack. • Allocated objects are created by allocation functions and live until freed. • Portable C describes storage duration and allocated storage rather than assuming machine layout.

💡 C Example

int automatic_value = 1; int *allocated_value = malloc(sizeof *allocated_value); if (allocated_value != NULL) { *allocated_value = 2; free(allocated_value); }

⚡ Quick Revision

In portable C, distinguish automatic storage duration from dynamically allocated storage.