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
68 practiced

Write or describe a C function that safely copies up to n bytes from an untrusted input buffer into a destination buffer, guarantees null termination when appropriate, and returns an error if truncation occurs. How would you test edge cases such as zero-length buffers and exact fits?

EasyTechnical
84 practiced

Write a small C function that reports whether the host is little-endian, and one that converts a 32-bit value to big-endian. What portability and strict-aliasing pitfalls should you avoid, and what bugs appear when code assumes host byte order matches the data format?

HardTechnical
76 practiced

A C library works at -O0 but fails only in optimized builds, and the failure vanishes under the debugger. How would you investigate whether undefined behavior from pointer arithmetic, out-of-bounds access, or misaligned access is the cause, and how would you prove your fix is correct?

EasyTechnical
77 practiced

This C code leaks memory in a long-running process: char *read_message(void){ char *buf = malloc(128); /* fill */ return buf; } void loop(void){ char *m = read_message(); process(m); }. Explain why it leaks, fix it, and describe a better ownership design for the interface.

HardTechnical
84 practiced

You are designing a C event-loop API that stores a callback and a user-data pointer for later invocation. The callback may run long after registration, and callers can unregister at any time. What rules or safeguards would you put in place so the callback does not dereference freed memory?

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.