IBM Interview Question for Developer Program Engineers






Comment hidden because of low score. Click to expand.
1
of 1 vote

class Myclass
{
public:
int fun();
};


Sizeof(Myclass) is 1 byte ( same as empty class size)


And


class Myclass
{
public:
vitual int fun();
};


Sizeof(Myclass) is 4 bytes ( int size it will take)

- viswathsen April 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can write a code to get the answer. But can you explain why?

- Lion April 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The minimum size u need to represent an object is 1 byte. That's why first one returned 1 byte.
Second one contains a Vtbl entry that's why the answer is 4.

- Jags April 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The second one will have a size of a pointer, which can be 8 bytes (on x64)

- kilotaras April 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

first case: 1 byte so that no two objects have the same address in the memory

second case: since it has a virtual function, the compiler will add a virtual table and a virtual pointer...but since each class has one virtual pointer per class and not per object, the size of the object should still be 1 bytes...

any suggestions?

- camSun April 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think virtual pointer is declare in first class in hierarchy who has introduce virtual function.

Now any derive class that override base class virtual function or introduces new virtual function they use same virtual pointer. And update pointer in constructor & destructor of class.

And virtual pointer take size of one pointer unit (32 bit for 32 bit machine) in every object.

- Divyang J. Mithaiwala April 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In my opinion, as everyone has already agreed that a class with virtual function requires a virtual table. And we have to store pointer to that virtual table somewhere for the class. Now as we know a pointer has a size of 4 bytes (depending on the underlying hardware, i.e. how many bits are required to represent a physical address). So class with virtual function has a size of 4 bytes.

- Abhishek Soni April 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Should n't we add size of empty class + size of vtable pointer in second case. in that case ans for second case is 5 bytes

- Anonymous October 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If we have vtable pointer inside a class, we do not need anymore 1 byte difference to prevent object creation at the same place in memory. Thus, the size remains 4 bytes (i.e. more that 1 byte).

- sergey.a.kabanov January 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, you are wrong. Each class with virtual function has a vtble and virtual pointer for each object it instantiates. Thats why each object is associated with a separate vtbl pointer. Hence its size is 4 bytes.
Note just a class has no size. Class is merely a blue print. Its objects have sizes.

- Sagnik Majumder January 31, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More