Language-Level Memory Management (C/C++/Rust) Questions

How C and C++ expose and manage memory at the language level: pointers and pointer arithmetic, pointers to pointers and function pointers, arrays versus pointers and array decay, stack versus heap storage, manual allocation and freeing in C (malloc, realloc, free), new and delete versus malloc and free, placement new, RAII and owning smart pointers (unique_ptr, including custom deleters for C resources), shallow versus deep copies and move semantics in C++. Covers reasoning about who owns a buffer and designing ownership and lifetime contracts across function, library and plugin boundaries; dangling and uninitialized pointers, leaks, double frees, use-after-free, off-by-one errors and buffer overruns and how to prevent them; undefined behavior, strict aliasing and safe byte reinterpretation, endianness; struct layout, alignment and padding as the language defines them; and finding and diagnosing memory bugs with sanitizers, Valgrind-style tools, tracing allocators and heap-corruption triage, including allocator design and heap fragmentation at the language level. Boundary: garbage-collector behavior and tuning, embedded memory budgets, register access and packing structs to hardware layouts, OS virtual memory and paging, and lock-based concurrency are covered elsewhere.

MediumTechnical
87 practiced

A parser builds a heap-allocated char ** list of tokens and hands it to the caller. Some tokens are duplicated strings, and some are slices into a larger buffer. How would you design the ownership rules and cleanup path so the caller can safely filter or free the result without leaks or double frees?

MediumTechnical
83 practiced

A C function returns a pointer to a buffer. What are the ownership and lifetime implications when it returns a pointer to a local automatic variable, a pointer to a static local variable, and a pointer obtained from malloc, and how would you document or enforce who must free what?

MediumTechnical
89 practiced

Which tools and compiler or runtime options would you use to find memory leaks, out-of-bounds accesses, and use-after-free bugs in a C or C++ codebase, and how do you interpret their output when triaging a suspected bug?

HardSystem Design
77 practiced

Design a C or C++ plugin interface where a host loads third-party modules and passes buffers and callbacks across the module boundary. What ownership and lifetime contract would you enforce between host and plugin?

MediumTechnical
64 practiced

Compare C-style manual resource cleanup with RAII in C++. In a codebase where exceptions are disabled, what idiomatic patterns manage resources deterministically, and what would a small RAII wrapper look like?

Unlock Full Question Bank

Get access to all 44 Language-Level Memory Management (C/C++/Rust) interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.