← NVIDIA Interview Insights

NVIDIA·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

Got a low-level C++ memory question at NVIDIA that looked simple on the surface but required knowing struct padding rules cold. Not a lot of context in what I have, but it was the kind of thing where if you haven't thought about alignment before, you're just guessing.

Questions Asked (1)

Q1

Given a class with members `char buf[BUFF_SIZE]` (where BUFF_SIZE is 1), `size_t length`, and `char *ptr`, what is the result of `sizeof` on that class? Explain using struct alignment and padding.

Technical Trade-offsSystem Design
Author's notes

This one tripped me up because I jumped straight to adding up the raw sizes: 1 byte for the char array, 4 bytes for size_t, 4 bytes for the pointer on 32-bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, identify the size and alignment requirements of each member: char buf[1] (size 1, alignment 1), size_t length (typically 8 bytes on 64-bit, alignment 8), and char* ptr (typically 8 bytes on 64-bit, alignment 8). Then, apply struct layout rules: members are laid out in order with padding inserted to satisfy each member's alignment, and the total size is padded to a multiple of the struct's alignment (the maximum alignment among members). Finally, compute the total size, noting that the exact result depends on the platform's size_t and pointer sizes.

Pro tip: Mention that the answer is platform-dependent and explicitly state your assumptions (e.g., 64-bit system with 8-byte size_t and pointers). This shows you understand that such questions test reasoning about alignment, not memorization.

1. Determine member sizes and alignments

List each member with its size and alignment requirement: char buf[1] (1 byte, align 1), size_t length (typically 8 bytes, align 8 on 64-bit), and char* ptr (typically 8 bytes, align 8 on 64-bit).

2. Lay out members with padding

Place buf at offset 0. To align length to 8, insert 7 bytes of padding, so length starts at offset 8. Then place ptr at offset 16 (no padding needed since length ends at 16).

3. Compute total size and final padding

The sum of member sizes plus internal padding is 1 + 7 + 8 + 8 = 24 bytes. Since the struct's alignment is 8 (max of members), and 24 is a multiple of 8, no trailing padding is needed. Thus sizeof(class) = 24 bytes.

4. Discuss platform dependence and variations

Note that on 32-bit systems, size_t and pointers are 4 bytes, so the layout would be: buf at 0, 3 bytes padding, length at 4, ptr at 8, total size 12 bytes (multiple of 4). Also mention that compiler-specific packing or attributes could alter the result.

Key Points to Mention

  • Struct alignment rules: members are aligned to their natural alignment, and the struct's total size is a multiple of its maximum alignment.
  • Padding is inserted between members to satisfy alignment requirements, and possibly at the end to ensure the total size is a multiple of the struct's alignment.
  • The size of size_t and pointers is platform-dependent (e.g., 8 bytes on 64-bit, 4 bytes on 32-bit), which affects the result.
  • The order of members affects padding; reordering members from largest to smallest can minimize padding (though not applicable here due to the array).
  • The result is not simply the sum of member sizes; it includes padding bytes.
  • Compiler-specific pragmas (e.g., #pragma pack) or attributes can change alignment and thus the size.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.