Embercode
✕
Pointers and Memory AddressesQuick quiz

Layout and padding

8 questions. Covers Struct padding and object sizes.

Questions 1 to 5 of 8

  1. 1. What does this program print? ``cpp #include <iostream> struct Bad { char tag; int count; char flag; }; struct Good { int count; char tag; char flag; }; int main() { std::cout << sizeof(Bad) << ' ' << sizeof(Good) << '\n'; } ``
  2. 2. What does this program print? ``cpp #include <iostream> int main() { int grid[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, }; const int* flat = &grid[0][0]; std::cout << grid[2][1] << ' ' << (&grid[2][1] - flat) << '\n'; } ``
  3. 3. What does this program print? ``cpp #include <cstddef> #include <iostream> struct Pair { int a; char b; }; int main() { std::cout << offsetof(Pair, b) << ' ' << sizeof(Pair) << '\n'; } ``
  4. 4. In row-major layout, the position of cell [r][c], counted from zero, is r * ___ + c. What goes in the blank?

    Type your answer.

  5. 5. Which name from <cstddef> gives the byte position of a named field inside a struct? Answer with the name only.

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.