# Sizeof problems(interview questions)

Discussion in 'C++' started by maxmus@gmail.com, Jul 8, 2005.

1. ### Guest

Class A
{
int a;
};

cout<<"the size of A is "<< sizeof(A)<<endl;

Q: what is the result?

Now Add in a function, int getVal();

What's the result now?

Explain why

, Jul 8, 2005

2. ### Artie GoldGuest

wrote:
> Class A
> {
> int a;
> };

Syntax error. What is `Class'? ('class', we know; C++ is case sensitive.)
>
> cout<<"the size of A is "<< sizeof(A)<<endl;

Assuming that `A' has been declared correctly above, the answer is
`whatever sizeof(A) *says* it is.
>
> Q: what is the result?
>
> Now Add in a function, int getVal();
>
> What's the result now?

Similarly.

>
> Explain why
>

Because doing your own homework is something called, well, not cheating.

--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays

Artie Gold, Jul 8, 2005

3. ### Jack KleinGuest

On 7 Jul 2005 18:25:02 -0700, wrote in comp.lang.c++:

>
> Class A
> {
> int a;
> };
>
> cout<<"the size of A is "<< sizeof(A)<<endl;
>
> Q: what is the result?

What did you answer? The only answer that the C++ language provides
is that sizeof(A) is greater than or equal to sizeof(int). In
numbers, 1 or greater.

> Now Add in a function, int getVal();
>
> What's the result now?

The answer to the first question is equally accurate for the second,
still >= sizeof(int), still >= 1.

> Explain why

Because. Further explanation is pointless, as is trying to impress
whoever it is came up with this as a useful question.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Jack Klein, Jul 8, 2005
4. ### Guest

Come on. I am providing a question for people to practise/discuss, not

people.

, Jul 8, 2005
5. ### Guest

You haven't answered the question yet.
Greater or equal to sizeof(int)?
Does the later "adding " affect the result at all?

, Jul 8, 2005
6. ### MeGuest

wrote:
> class A
> {
> int a;
> };
>
> cout<<"the size of A is "<< sizeof(A)<<endl;
>
> Q: what is the result?

Prints out a number >= sizeof(int)

> Now Add in a function, int getVal();
>
> What's the result now?

Prints out the same thing.

> Explain why

A is still POD.

Me, Jul 8, 2005
7. ### Jack KleinGuest

On 7 Jul 2005 19:33:38 -0700, wrote in comp.lang.c++:

> You haven't answered the question yet.
> Greater or equal to sizeof(int)?
> Does the later "adding " affect the result at all?

Usually I don't even answer people who can't even be bothered to quote
correctly. If you make the choice to use the broken Google Groups
interface instead of a proper newsreader, it is still up to you to
quote properly.

And yes, I answered both questions. Learn how to read. And how to
quote.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Jack Klein, Jul 8, 2005
8. ### Victor BazarovGuest

Me wrote:
> wrote:
>> class A
>> {
>> int a;
>> };
>> [..]

>
> A is still POD.

No, it ain't. 'a' is private, and that makes it non-POD.

V

Victor Bazarov, Jul 8, 2005
9. ### David WhiteGuest

wrote:
> You haven't answered the question yet.

Well, answers were given. I assume you asked the questions because you don't
know the answers, so how do you know that the answers you were given are

DW

David White, Jul 8, 2005
10. ### MeGuest

Victor Bazarov wrote:
> Me wrote:
> > wrote:
> >> class A
> >> {
> >> int a;
> >> };
> >> [..]

> >
> > A is still POD.

>
> No, it ain't. 'a' is private, and that makes it non-POD.

Bleh. Well, so anyway, the reason why adding a non-virtual function
can't add space is 9.2/12 "so might requirements for space for managing
virtual functions (10.3) and virtual base classes (10.1)." neither of
which apply here.

Me, Jul 8, 2005
11. ### Guest

wrote:
> You haven't answered the question yet.
> Greater or equal to sizeof(int)?
> Does the later "adding " affect the result at all?

He DID answer your question. Let me reformulate, since you don't seem
to understand. The C++ language says this:

- sizeof(int) >= 1
- The size of any class is greater or equal to size of every member in
the class.
- If the class has any virutal methods then its size may be greater.

That's it. There's no way of knowing more precisely what sizeof(A) is.
If someone at an interview expected you to provide a more precise