3

Hey everyone I went to the interview to a well known global firm for position of C++ developer. I had a multiple choice test of 30 questions which I answered most of them correctly but some of them were tricky and badly worded . I had a lot of time so I wrote down one of the questions which in my opinion was badly worded so there is the exact copy of the question . I just want to know if it is me or the question is intentionally badly worded. I believe that every point is count because of competition for a position.

question :

What shall the flowing call return ?

sizeof(obj1);  

A. size of member functions of obj1 in bytes
B. size of the data in obj1.
C. size of member functions and the data of obj1.
D. none of the answers are correct.

I knew the answer should be size of object in bytes and I chose option C . In my opinion object contain the member functions (code segment) and data (static and dynamic allocation). The tester marked it as wrong answer .

David Yachnis
  • 484
  • 5
  • 14
  • It's definitely not choice C: see [here](http://stackoverflow.com/questions/6552319/c-sizeof-of-a-class-with-functions) – yizzlez Jul 15 '15 at 01:11
  • what is the answer in your opinion ? – David Yachnis Jul 15 '15 at 01:17
  • The closest answer (B) is badly worded for the reasons expressed in the comments in the below answer. I would say it returns "the number of bytes in `obj1`". – David G Jul 15 '15 at 01:33
  • 1
    @0x499602D2 When the closest choice is still too inaccurate and imprecise, I would choose D. –  Jul 15 '15 at 01:37
  • A and C are hopeless. B is far from consistently correct. So the best answer from an expert point of view is D. But I wouldn't be surprised if whoever invented the test defined B as the correct answer. – JSF Jul 15 '15 at 02:09
  • 1
    FWIW, `sizeof(obj1)` isn't a "call"... it's an operator working on an unnecessarily-parenthesised expression. Combined with the lack of an answer mentioning padding, and potential pointers to virtual dispatch tables (which you may or may not consider "data"), or a "write your own answer" option for D that would let someone marking the test know why you rejected B, it's sloppy. – Tony Delroy Jul 15 '15 at 02:17
  • The problem with "Multiple Choice" questions is that they hint at HR doing the testing. And a HR droid doesn't know what "padding" is. Still, this question tests a relevant skill. A good developer should not only understand C++ but also people and organizations. The "correct" answer to this question here cannot be given as we haven't met the interviewer and you haven't told us the organization. – MSalters Jul 15 '15 at 09:40
  • yeah.. I just discovered that the answer is actually D..not B ,but u can't initialize objects of an abstract class in the first place not to even talk of finding it's size,so let's keep virtual functions out of this question...but the size of the object consists of more than just data in its structure or class – Williams Maaki Sep 19 '18 at 10:28

3 Answers3

3

The tester was correct; the member functions would not be considered part of the size of the object, as they are shared across all instances of that class.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • @DavidYachnis But sizeof on an empty object `struct A {}` returns 1 – yizzlez Jul 15 '15 at 01:16
  • 2
    @DavidYachnis, In addition, I wouldn't consider padding bytes to be data of the object, but those are in there as well. If I had to guess, I'd wager the expected answer was B, though. – chris Jul 15 '15 at 01:23
  • 2
    Bit of a pedantic point, but re *"the member functions would not be considered part of the size of the object"* - true "as they are shared across all instances of that class."* - not so true, as code for inline functions is instead spread across the call sites, and uncalled functions may not make it into the executable image. – Tony Delroy Jul 15 '15 at 02:23
2

The answer is D because non of the above is correct. Padding is not part of the data. Consider the following two examples:

#include <iostream>

struct test
{
    int x;
    int y;
    double d;
    test(){}
};

int main()
{
    test t;
    std::cout << sizeof(t) << std::endl;

    std::cin.get();
    return 0;
}

will return 16 bytes of the data types + padding while:

#include <iostream>

struct test
{
    int x;
    double d;
    int y;
    test(){}
};

int main()
{
    test t;
    std::cout << sizeof(t) << std::endl;

    std::cin.get();
    return 0;
}

return 24 bytes for the data types + padding. So an int is not larger just because the data alignment is different. so its not B nor the other answers, ergo D is the closest.

1

The answer is D. It returns the size the object occupies in memory, which should be at least 1 by definition. This size includes data, virtual function tables, and padding bytes. Member functions don't occupy space, they are just functions that take 'this' as the first argument. A 'new' operator actually uses malloc with sizeof.

edwinc
  • 1,658
  • 1
  • 14
  • 14