Goldman Sachs Interview Question
Applications DevelopersCountry: India
Interview Type: In-Person
To ensure that the addresses of two different objects will be different.that is the reason "new" always returns pointers to distinct objects.
You can find the info at stroustrup's bs_faq2
Google for stroustrup's bs_faq2. Links are not allowed on Careercup.
For the third question, does it really depends on machine architecture? I don't think so, because it has to allocate min memory (i.e one bye) to distingush between two objects. Except that I don't see any other reason. If that is the case, why it depend on the archtecture. Byte is always in any machine.
Size of an empty class is not zero. It is 1 byte generally. It is nonzero to ensure that the two different objects will have different addresses. See the following example.
#include<iostream>
using namespace std;
class Empty { };
int main()
{
Empty a, b;
if (&a == &b)
cout << "impossible " << endl;
else
cout << "Fine " << endl;
return 0;
}
Output:
Fine
the class can never be empty as it is been extended by the java.lang.Object - So the size of the object can never be zero , because whenever the class is created in that case that class will get extended by the java.lang.Object ..hence which says that the class size can never be zero
- See if the class if empty in that case by default the methods would be like -- constructor and destructor
1. The standard does not allow objects (and classes thereof) of size 0, since that would make it possible for two distinct objects to have the same memory address. That's why even empty classes must have a size of (at least) 1.
- Expressions April 09, 20132. Default constructor, Copy constructor, Copy-assignment, Destructor, Move constructor, Move-assignment operator
3. Yes it depends on CPU architecture. It will at least be 1 byte.