Interview Question


Country: United States




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

sizeof(A) = 16 = 2 * sizeof(int) + sizeof ( pointer to virtual table ) + [ sizeof(char) + 3 padding]

- adrian.chitescu90 August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

size is 16
I am trying so see what happens by commenting different parts of the question.

When it is empty class. commenting virtual destructor and 2 ints and a char. it gives 1. (Its simple)
now including the char in the class, rest of the part is same. Then it gives 1. (Again justified)
But if we include the virtual destructor then size becomes 8. If that because of padding ?? or anything else!! Size due to virtual destructor is 4. 2 ints (4*2).

But size of char suddenly changes from 1 to 4?? Is it for padding. I think so. Correct me if I am wrong.

- Psycho August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Char only needs 1-byte alignment, and int or pointers need 4-byte alignment.
Structures and classes are aligned to the maximum of this values.

- adrian.chitescu90 August 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can someone plz tell me what does 4 byte allignment in 32bit system mean.
eg :
struct
{
char a;
char b;
}
will it take 8 byte or 4 byte after padding

- guest August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it will take 4 bytes. It should be multiple of 4

- Psycho August 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

so u mean char a - 1+ 3 for padding char b 1+ 3 for padding
so entire structure takes 8 byte?
or char a 1 char b 1 + 2 padding.=4byte total?
doesn't 4 byte allignment mean all new var address should start with a multiple of 4.... ?
plz do help

- guest August 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

So what is the size of the class if it is
Class A
{
Public:
A();
Virtual ~A();
char a;
Char b;
Int c;
}

- suhaskulkarni100 September 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sizeof(A) = 12 = sizeof ( pointer to virtual table ) + [ 2*sizeof(char) + 2 padding] + sizeof(int);

- adrian.chitescu90 September 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what is padding and it's significance

- sastry.soft October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Padding is required to keep the size of structs in multiples of 2. That way, it is easier to allocate memory and store in memory.

- crystal.rishi2 October 17, 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