TRT
Sign in
easyAdvancedconcept

Why use getaddrinfo instead of gethostbyname?

Explain protocol independence, IPv4/IPv6 dual-stack, thread safety, and struct addrinfo linked-list results.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstProtocol-Independent Networking: getaddrinfo and Dual-Stack IPv4/IPv6Pro — members onlyBuild protocol-independent network software using getaddrinfo, traverse DNS resolution lists, resolve service ports, and deploy dual-stack IPv4/IPv6 sockets.

A strong answer

A strong candidate frames Why use getaddrinfo instead of gethostbyname? around the exact memory layout, compiler semantics, and hardware execution model covered in Protocol-Independent Networking: getaddrinfo and Dual-Stack IPv4/IPv6. Explain protocol independence, IPv4/IPv6 dual-stack, thread safety, and struct addrinfo linked-list results.

This code works when communicating strictly over IPv4 using raw decimal IP addresses. However, modern network architectures expose three critical limitations:

  1. Protocol Rigidity: If the server migrates to an IPv6 address (2001:db8::1), this code fails to compile or connect. Supporting IPv6 requires rewriting the program with struct sockaddr_in6, AF_INET6, and separate socket logic.
  2. Thread-Unsafe Legacy Resolvers: Historically, developers used gethostbyname("example.com") to resolve domain names. gethostbyname() returns a pointer to a single static internal buffer. If two threads call gethostbyname() concurrently, they overwrite each other's DNS results. Furthermore, gethostbyname() is completely incapable of returning IPv6 addresses.
  3. Dual-Stack Operating Environments: Modern cloud data centers, cellular carriers, and ISPs deploy dual-stack networks or pure IPv6 backbones with IPv4 translation (NAT64/DNS64). Running two separate server processes on separate ports—one for IPv4 and one for IPv6—wastes system memory, complicates monitoring, and duplicates connection logic.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Eliminate protocol hardcoding using getaddrinfo and freeaddrinfo.
  • Mastery of core invariant: Iterate through resolved addrinfo linked lists to establish resilient socket connections.
  • Mastery of core invariant: Perform reverse DNS lookups and format host/service strings using getnameinfo.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How does the compiler, OS kernel, or hardware memory model enforce the invariants of Protocol-Independent Networking: getaddrinfo and Dual-Stack IPv4/IPv6 under optimization (`-O2`/`-O3`)?
  2. 02What production bug or undefined behavior occurs when Protocol-Independent Networking: getaddrinfo and Dual-Stack IPv4/IPv6 is misused across module or thread boundaries, and which sanitizer (`ASan`, `UBSan`, `TSan`) catches it?

Not started

Sign in to save your learning progress.

Sign in to save