Katkat

Explain C++ alignment and ABI stability

DiscordSoftware Engineer · HR Screen

Last updated

This question evaluates mastery of C++ low-level memory concepts—memory alignment, padding and struct/class layout—and understanding of ABI stability, including calling conventions, name mangling, vtable layout and other binary compatibility concerns, reflecting competence in systems-level C++ and binary interfacing.

You are interviewing for a senior C++ / systems role. The interviewer asks two low-level fundamentals questions:

  1. C++ memory alignment and padding
  • Explain what memory alignment means in C and C++.
  • Describe what padding is inside and at the end of structs/classes.
  • Show with a small example how the order of data members can change the sizeof a struct due to padding.
  • Discuss why alignment and padding exist (performance, hardware constraints) and what problems they can cause (e.g., unexpected struct size, binary compatibility issues, undefined behavior if misaligned).
  • Mention typical ways to inspect or control layout when absolutely necessary (e.g., compiler-specific pragmas/attributes, static_assert(sizeof(...)), etc.), and the risks of doing so.
  1. ABI (Application Binary Interface) and its instability
  • Define what an ABI (Application Binary Interface) is, and how it differs from a source-level API.
  • Explain which aspects are covered by an ABI (e.g., calling conventions, data layout, alignment, name mangling, exception handling, vtable layout).
  • Explain why the C++ ABI is often not stable across different compilers or even different versions of the same compiler, while C ABIs tend to be much more stable.
  • Give concrete examples of problems ABI instability can cause in practice (e.g., linking a program against a shared library built with a different compiler/version, plugin systems, distributing precompiled libraries).
  • Describe common engineering strategies to mitigate ABI issues in C++ (e.g., using a stable C interface boundary with extern "C", avoiding STL types in public ABI, the PImpl idiom, rebuilding all components with the same toolchain).

Explain your answers in a way that shows deep understanding of low-level details and practical trade-offs.