Katkat

Explain C++ Inline, Segfaults, Virtual Memory, and std::string Internals

Hudson River TradingSoftware Engineer · Technical Screen

Last updated

Tests deep understanding of low-level systems and modern C++ fundamentals, focusing on compiler inlining mechanics, virtual memory paging, segmentation fault root causes, and standard library string memory layouts.

Context

In latency-sensitive and systems-level C++ programming, engineers must understand not only the syntax of the language, but also how high-level abstractions map down through the compiler, standard library, operating system kernel, and hardware memory subsystem.

Answer the following four core questions with precise, implementation-level technical details.

Questions

1. Inlining and the inline Keyword

  • Explain the mechanical difference between what the inline keyword guarantees to the linker versus what it suggests to the compiler optimizer.
  • Under what conditions might a compiler refuse to inline a function even if explicitly requested (e.g., via __attribute__((always_inline)) or inline)?
  • Discuss the cache-level trade-offs (instruction cache / L1i pressure vs. call overhead and optimization opportunities) when inlining aggressively.

2. Segmentation Faults and Virtual Memory

  • Trace the exact sequence of events—from the CPU issuing an instruction to the kernel delivering SIGSEGV—when a process dereferences an invalid virtual address (e.g., nullptr or an unmapped page).
  • Explain how page tables, translation lookaside buffers (TLBs), page fault exceptions (interrupts), and OS page table walks interact during this event.
  • Distinguish between a hardware-level page fault that resolves transparently (minor/major fault) and one that leads to a segmentation fault.

3. Virtual Memory Architecture

  • Explain why modern operating systems use virtual memory rather than physical addressing directly.
  • Describe the multi-level page table structure (e.g., 4-level paging on x86-64), including how virtual addresses are decomposed into offsets and indices.
  • What are huge pages (2MB / 1GB), and how do they impact TLB hit rates and latency?

4. std::string Internals and Small String Optimization (SSO)

  • Diagram or describe the internal memory layout of a typical modern std::string implementation (such as libc++ or libstdc++).
  • Explain Small String Optimization (SSO): how the union between inline buffer and heap pointer is structured, typical capacity limits before heap allocation occurs, and the impact of SSO on move semantics and pointer invalidation.

Hints

Hint 1

Recall that the modern C++ definition of the `inline` keyword primarily alters the One Definition Rule (ODR) across translation units rather than strictly controlling code generation.

Hint 2

A segmentation fault begins as a hardware exception triggered by the MMU when a page table entry lacks the required present or permission bits for the requested operation.

Hint 3

For SSO, consider how a standard 24-byte or 32-byte string object can repurpose pointer and capacity member fields into an inline `char` array and a flag/size byte.

Follow-up questions

  • How does Link-Time Optimization (LTO) alter the scope and capabilities of the compiler's inlining decisions across translation units?
  • If a process encounters a write fault on a valid, mapped page marked read-only, how does the OS handle Copy-on-Write (CoW) versus dispatching a `SIGSEGV`?
  • Why does moving an SSO-allocated `std::string` have different performance characteristics and pointer invalidation guarantees compared to moving a heap-allocated `std::string`?