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.

EasyTechnical
115 practiced

How does an array differ from a pointer in C, particularly when passed to a function? Why does array-to-pointer decay lead to length-handling and bounds-check bugs, and what does sizeof return in each case?

EasyTechnical
87 practiced

What is undefined behavior in C and C++, and why is it especially dangerous in code that handles untrusted input? Give examples that look harmless in review but can become exploitable or non-deterministic at runtime.

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?

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?

HardTechnical
83 practiced

A C++ service in production shows steady memory growth under certain traffic that you cannot reproduce locally, and you have no full heap dumps. How would you confirm it is a leak, isolate the leaking allocation sites with lightweight monitoring or sampling, and roll out a fix safely?

Unlock Full Question Bank

Get access to all 36 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.