2 Replies - 842 Views - Last Post: 02 April 2011 - 08:20 AM Rate Topic: -----

#1 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Cool interview question

Posted 02 April 2011 - 06:00 AM

the following code will output
00000
01010

class base          
{
public:
int bval;
base(){ bval=0;}
};
 
class deri:public base
{
public:
int dval;
deri(){ dval=1;}
};
void SomeFunc(base *arr,int size)
{
for(int i=0; i‹size; i++,arr++)
cout«arr-›bval;
cout«endl;
}
 
int main()
{
base BaseArr[5];
SomeFunc(BaseArr,5);
deri DeriArr[5];
SomeFunc(DeriArr,5);
}


the explication for this can be found here: http://technical-int..._answer_25.aspx

So I modified the function so that it will output me:
00000
00000

like this:
void SomeFunc(base *arr,int size, int o)
{
	int k=1;
if (o==2)  k=sizeof(deri)/sizeof(base); //works only for perfect div
for(int i=0; i<size; i++)
{
cout<<arr->bval;
arr+=k;
}
cout<<endl;
}

int main()
{
base BaseArr[5];
SomeFunc(BaseArr,5,1);
deri DeriArr[5];
SomeFunc(DeriArr,5,2);
}




but I'm wondering if is there a better way to do it, using the same original function?
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Cool interview question

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Cool interview question

Posted 02 April 2011 - 08:06 AM

View Postlivium, on 02 April 2011 - 01:00 PM, said:

the following code will output
00000
01010
I'm afraid that the link you provided is wrong - code which relies on undefined behaviour does not have defined output. the output you see may be what it does on your compiler, however the standard provides absolutely no guarantees about the sizeof(base) or the sizeof(deri). In other words, you cannot assume that sizeof(deri) is equivalent to sizeof(base)+sizeof(int), because there are all kinds of things which your compiler may or may not do behind-the-scenes which affect the sizeof a class/struct/union type

Assuming that the question is along the lines of "how does this code work?", the correct answer to this interview question is "the output is undefined", unless you specify exactly how the compiler which you use to build the code actually lays out class data members in memory (including padding etc).

If you were presented this in a real interview, you might also get bonus points for pointing out how replacing 'raw' arrays in C++ with vectors can prevent programmers from making mistakes like this.


Of course, you could rephrase the interview question to ask "how might this code produce this output?", in which case the answer could be valid for a particular implementation.


In response to your actual question; I don't think there's any valid answer to it - the bottom line is that the program is showing undefined behaviour, so 'refactoring' it to what would most likely involve more undefined behaviour is most likely only to have unpredictable/unstable results.

This post has been edited by Bench: 02 April 2011 - 08:13 AM

Was This Post Helpful? 1
  • +
  • -

#3 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: Cool interview question

Posted 02 April 2011 - 08:20 AM

So the answer to my question is that one cannot modify the code so that the output might be 00000 00000 without making use of the fact that the function knows to what (derived/base) class the base class pointer might point to.

Thanks for your answer!

Ok, thanks again.

This post has been edited by livium: 02 April 2011 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1