SW Architect Interview Question

Q. How does free() work internally?

free() releases dynamically allocated heap memory. The memory allocator maintains metadata for each allocated block. When free(ptr) is called, it locates the block's metadata, marks the block as free, and places it back into the allocator's free list so future malloc() calls can reuse it. The pointer itself is not modified, so it becomes a dangling pointer unless it is explicitly set to NULL. Calling free() on the same pointer twice results in undefined behavior, while free(NULL) is always safe.

Comments

Popular posts from this blog

Embedded C Interview (50 Questions)

Fundamental Secure Features in Automotive ECUs

Overview of ISO/SAE 21434 Standard