Velocity Reviews > C++ > Sizeof problems(interview questions)

# Sizeof problems(interview questions)

maxmus@gmail.com
Guest
Posts: n/a

 07-08-2005

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

Artie Gold
Guest
Posts: n/a

 07-08-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

Jack Klein
Guest
Posts: n/a

 07-08-2005
On 7 Jul 2005 18:25:02 -0700, (E-Mail Removed) 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/~a...FAQ-acllc.html

maxmus@gmail.com
Guest
Posts: n/a

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

people.

maxmus@gmail.com
Guest
Posts: n/a

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

Me
Guest
Posts: n/a

 07-08-2005
(E-Mail Removed) 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.

Jack Klein
Guest
Posts: n/a

 07-08-2005
On 7 Jul 2005 19:33:38 -0700, (E-Mail Removed) 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/~a...FAQ-acllc.html

Victor Bazarov
Guest
Posts: n/a

 07-08-2005
Me wrote:
> (E-Mail Removed) wrote:
>> class A
>> {
>> int a;
>> };
>> [..]

>
> A is still POD.

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

V

David White
Guest
Posts: n/a

 07-08-2005
(E-Mail Removed) 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

Me
Guest
Posts: n/a

 07-08-2005
Victor Bazarov wrote:
> Me wrote:
> > (E-Mail Removed) 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.