Sizeof problems(interview questions)

M

maxmus

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
 
A

Artie Gold

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
 
J

Jack Klein

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.
 
M

maxmus

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

If you feel confident, just give your answer instead of laughing at
people.
 
M

maxmus

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

Jack Klein

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.
 
D

David White

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
inadequate? What sort of answers would satisfy you?

DW
 
M

Me

Victor said:
Me said:
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.
 
B

bart.kowalski

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
answer without specifying which platform they are talking about then
they are clueless and you shouldn't bother trying to get the job they
are offering.


Bart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top