Bloomberg LP Interview Question for Software Engineer / Developers






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

the key point of the question is, for an empty class, compiler will add default constructor/destructor/copy constructor/assignment operator to the class. you need to take that into consideration.

For virtual functions, there will be a vtbl defined in the class object, thus the size will become larger. i dont know how large it will be though.

- ron August 08, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the size of en empty class will be 1 byte. if virtual functions are added to the class, then the size becomes 4 bytes. it remains at 4 bytes irrespective of the number of virtual functions one adds. hope this helps....

Sridhar Poduri
Sr. Analyst
Verizon

- sridhar August 12, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is there any API for me to call to get the size of class?

- tt August 12, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can check this at :- http://www.research.att.com/~bs/bs_faq2.html
Why is the size of an empty class not zero?
To ensure that the addresses of two different objects will be different. For the same reason, "new" always returns pointers to distinct objects. Consider:
class Empty { };

void f()
{
Empty a, b;
if (&a == &b) cout << "impossible: report error to compiler supplier";

Empty* p1 = new Empty;
Empty* p2 = new Empty;
if (p1 == p2) cout << "impossible: report error to compiler supplier";
}

There is an interesting rule that says that an empty base class need not be represented by a separate byte:
struct X : Empty {
int a;
// ...
};

void f(X* p)
{
void* p1 = p;
void* p2 = &p->a;
if (p1 == p2) cout << "nice: good optimizer";
}

This optimization is safe and can be most useful. It allows a programmer to use empty classes to represent very simple concepts without overhead. Some current compilers provide this "empty base class optimization".

- Hitesh September 07, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I simply did not understand the basis of this question. Come on i have also worked in this technology for so many years and have used extensively. why would some one do that or ask such a question.
My Ans: I dont know
Q: what if the class has one empty function ? what would be its size ?
My Ans : Size would be incremented by 4 bytes on a 32 bit system than the previous size.
Q: what if the function is made virtual ? what would be its size ?
My Ans : Class Size would be the same.

- Alfy December 22, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The empty class size is 1 byte just so that 2 objects do not land up on the same address space.
Even if the class has an empty or non empty function, its object size will not increase since obj size is a result of the data members of the class.
Same applies for virtual func.

- Nikhil July 27, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how r u ppl stating it to be 4 Bytes for a virtual func.. & 1B for the empty class. Can anyone throw some light on it.

- aks February 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you mean the size of "an instance of an empty class", it's definitely one.

- Bill February 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Compiler will generate 1 byte of memory to mark the existence of the class.

The reason is that all classes must have a memory size of at least 1 byte so that the class doesn't occupy the same memory space with another class. This is to prevent name mangling. i.e. if I declare a class A {}; the compiler will still generate an entry in its table to something called "A". If behind that I declare another class say class B if A takes 0 bytes of memory and B's data gets written in the place where A was declared. In this case an instantiation of A would take on the properties of B.

- Vlad September 30, 2009 | Flag Reply


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