Interview questions

C

CFG

I still don't understand why you've picked this question?
Just for the sake of the fancy term "POD", which you already know?

This question is definitely not the best single question about C++.
 
I

Ioannis Vranos

CFG said:
I still don't understand why you've picked this question?
Just for the sake of the fancy term "POD", which you already know?

This question is definitely not the best single question about C++.


As I said it would be one question of the many to determine the depth of
the candidate's knowledge. There would be simpler ones too. Since he
asked for a question that he could be asked, I chose to provide a
difficult one and not an easy one, that it would be certain he would
answer. And he learned new things, so why is it bad?






Regards,

Ioannis Vranos
 
I

Ioannis Vranos

cj said:
Dear friends, I have one more questions for everyone in the newsgroup:

I am preparing for an interview on UNIX/C++. Could you please identify some
of the most important questions which might be asked, so that I could best
prepare for it?



Another interesting question that I would ask, in the advanced level
this one.




In a class hierarchy with virtual member functions, how much does the
time cost of calling a virtual function increases while the depth of
abstraction increases?


For example:


class base1
{
// ...
virtual void something();
};

class base2: public base1
{
// ...
void something();
};

//...

class base999999: public base999998
{
// ...
void something();
};


base1 *p1=new base78;

p1->something();


base1 *p2=new base999999;

p2->something();



How much more time it takes for the implementation to find and invoke
base999999::something() in comparison to base78::something()?






Regards,

Ioannis Vranos
 
E

E. Robert Tisdale

Ioannis said:
Another interesting question that I would ask
in the advanced level is this one.

In a class hierarchy with virtual member functions,
how much does the time cost of calling a virtual function increases
while the depth of abstraction increases?

For example:

class base1 {
// ... public:
virtual void something(void);
};

class base2: public base1 {
// ... public:
void something();
};

//...

class base999999: public base999998 {
// ... public:
void something();
};

base1 *p1=new base78;

p1->something();

base1 *p2=new base999999;

p2->something();

How much more time it takes
for the implementation to find and invoke base999999::something()
in comparison to base78::something()?

It depends upon the implementation (compiler).
In the typical implementation, it takes no more time.
The C++ computer programming language standard
does *not* specify implementations, performance or efficiency.
 
P

Peter Koch Larsen

Ioannis Vranos said:
cj said:
Dear friends, I have one more questions for everyone in the newsgroup:

I am preparing for an interview on UNIX/C++. Could you please identify some
of the most important questions which might be asked, so that I could best
prepare for it?

Thank you,
C++J



Here is one I would ask:


Is the following code guaranteed to be safe and portable?



#include <string>
#include <vector>
#include <cstddef>

int main()
{
using namespace std;

class A
{
vector<int>array;
string s;

public:
A():array(100){}
}a;


unsigned char *p=reinterpret_cast<unsigned char *>(&a);

unsigned char *v=new unsigned char[sizeof(a)];


for(size_t i=0; i<sizeof(a); ++i)
v=p;
}



I do believe that that question is a very bad one. The code above is
obviously portable, but these casts do confuse. What on earth are you going
to do with v? Any programmers instinct is that code must have "a use", and
it is quite difficult to see what you could portable do with v. Also, the
reinterpret_cast is to a non-const pointer which chould also give all of us
an uneasy feeling. Don't do that against those poor could-be collegues!

Kind regards
Peter
 
P

Peter Koch Larsen

Ioannis Vranos said:
cj said:
Dear friends, I have one more questions for everyone in the newsgroup:

I am preparing for an interview on UNIX/C++. Could you please identify some
of the most important questions which might be asked, so that I could best
prepare for it?



Another interesting question that I would ask, in the advanced level
this one.




In a class hierarchy with virtual member functions, how much does the
time cost of calling a virtual function increases while the depth of
abstraction increases?
[snip]


How much more time it takes for the implementation to find and invoke
base999999::something() in comparison to base78::something()?

Regards,

Ioannis Vranos

That one was much better! ;-)

/Peter
 
I

Ioannis Vranos

E. Robert Tisdale said:
It depends upon the implementation (compiler).
In the typical implementation, it takes no more time.
The C++ computer programming language standard
does *not* specify implementations, performance or efficiency.



Wrong. Next please. :)






Regards,

Ioannis Vranos
 
I

Ioannis Vranos

Ioannis said:
Wrong. Next please. :)


But I forgot to give you a reference. "The C++ Programming Language" 3rd
Edition or Special Edition, page 324.






Regards,

Ioannis Vranos
 
J

JKop

Peter Koch Larsen posted:
I do believe that that question is a very bad one. The code above is
obviously portable, but these casts do confuse. What on earth are you
going to do with v? Any programmers instinct is that code must have "a
use", and it is quite difficult to see what you could portable do with
v.

What ever happened to just having fun?

int main(void)
{
int monkeys[7] = { 3, 4 ,3, 2 ,2 ,34, 23 ,3 };

char jack reinterpret_cast<char>(monkeys[5]);

cout << jack;
}
 
J

JKop

JKop posted:
Peter Koch Larsen posted:
I do believe that that question is a very bad one. The code above is
obviously portable, but these casts do confuse. What on earth are you
going to do with v? Any programmers instinct is that code must have "a
use", and it is quite difficult to see what you could portable do with
v.

What ever happened to just having fun?

int main(void)
{
int monkeys[7] = { 3, 4 ,3, 2 ,2 ,34, 23 ,3 };

char jack reinterpret_cast<char>(monkeys[5]);
TYPO



cout << jack;
}
 
D

Default User

E. Robert Tisdale said:
Something that calls itself Default User wrote:

[nothing that has to do with C++.]

Unlike your post? Besides being a liar, you're a hypocrit.
Go away troll.

I'll make a deal with you. We'll have an on-line vote (not in the
group). Whoever gets the most votes as being a troll has to stop
posting.

You up for it, troll-boy?



Brian Rodenborn
 
P

pembed2003

Ioannis Vranos said:
Another interesting question that I would ask, in the advanced level
this one.




In a class hierarchy with virtual member functions, how much does the
time cost of calling a virtual function increases while the depth of
abstraction increases?


For example:


class base1
{
// ...
virtual void something();
};

class base2: public base1
{
// ...
void something();
};

//...

class base999999: public base999998
{
// ...
void something();
};


base1 *p1=new base78;

p1->something();


base1 *p2=new base999999;

p2->something();



How much more time it takes for the implementation to find and invoke
base999999::something() in comparison to base78::something()?

Hi,
As a new C++ programmer, I found your question very interesting. I
think it takes about the same time to find and invoke
base78::something() and base999999::something(), right? Because each
object carries a virtual table pointer (if the class has at least one
virtual function) with an bunch of virutal function pointers to the
possible function definition. The lookup (not sure how the actual
virtual table is implemented but if it's something like a binary tree
or hash table then...) thus takes appr. the same amount of time.

If I answered it incorrectly, can you provide the answer? I want to
learn.

Thanks!
 
E

E. Robert Tisdale

pembed2003 said:
As a new C++ programmer, I found your question very interesting.
I think it takes about the same time to find and invoke
base78::something() and base999999::something(), right?

It depends upon the implementation.
But, yes, you are correct for typical implementations.
Because each object carries a virtual table pointer
(if the class has at least one virtual function)
with an bunch of virutal function pointers
to the possible function definition. The lookup
(not sure how the actual virtual table is implemented
but if it's something like a binary tree or hash table then...)
thus takes appr. the same amount of time.

If I answered it incorrectly, can you provide the answer?
I want to learn.

Actually, in Ioannis Vranos' example,
it appears that p1 and p2 are defined in the same scope
as the respective subsequent invocations of something(void).
In this case, a good optimizing C++ compiler will emit code
to invoke the correct function directly
and will not even consult any virtual function table.
 
I

Ioannis Vranos

pembed2003 said:
Hi,
As a new C++ programmer, I found your question very interesting. I
think it takes about the same time to find and invoke
base78::something() and base999999::something(), right? Because each
object carries a virtual table pointer (if the class has at least one
virtual function) with an bunch of virutal function pointers to the
possible function definition. The lookup (not sure how the actual
virtual table is implemented but if it's something like a binary tree
or hash table then...) thus takes appr. the same amount of time.

If I answered it incorrectly, can you provide the answer? I want to
learn.


Yes the time cost is the *same* regardless the depth of the abstraction.



As it is written in TC++PL on page 324:


"Classical hierarchies do tend to couple implementation concerns rather
strongly with the interfaces provided to users. Abstract classes can
help here. Hierarchies of abstract classes provide a clean and powerful
way of expressing concepts without encumbering them with implementation
concerns or significant run-time overheads. After all, a virtual
function call is cheap and independent of the kind of abstraction
barrier it crosses. It costs no more to call a member of an abstract
class than to call any other virtual function."






Regards,

Ioannis Vranos
 
P

pembed2003

JKop said:
JKop posted:
Peter Koch Larsen posted:
I do believe that that question is a very bad one. The code above is
obviously portable, but these casts do confuse. What on earth are you
going to do with v? Any programmers instinct is that code must have "a
use", and it is quite difficult to see what you could portable do with
v.

What ever happened to just having fun?

int main(void)
{
int monkeys[7] = { 3, 4 ,3, 2 ,2 ,34, 23 ,3 };

char jack reinterpret_cast<char>(monkeys[5]);
TYPO



cout << jack;
}

Hi,
I am trying to compile your code using g++ in FreeBSD. g++ -v gives:

gcc version 2.95.3 20010125 (prerelease)

the whole program looks like:

#include<iostream>
using namespace std;
void main(void){
int monkeys[8] = {3,4,3,2,2,34,23,3};
char jack = reinterpret_cast<char>(monkeys[5]);
cout<<jack<<endl;
}

but it won't compile:

g++ tmp.cpp -o tmp
tmp.cpp: In function `int main(...)':
tmp.cpp:5: reinterpret_cast from `int' to `char'

I am trying to understand your program but failed. Can you tell me
what you are trying to demonstrate and what the program is supposed to
do? I am trying to learn.

Thanks!
 
I

Ioannis Vranos

pembed2003 said:
Hi,
As a new C++ programmer, I found your question very interesting. I
think it takes about the same time to find and invoke
base78::something() and base999999::something(), right? Because each
object carries a virtual table pointer (if the class has at least one
virtual function) with an bunch of virutal function pointers to the
possible function definition. The lookup (not sure how the actual
virtual table is implemented but if it's something like a binary tree
or hash table then...) thus takes appr. the same amount of time.

If I answered it incorrectly, can you provide the answer? I want to
learn.

Thanks!


I had problems with my server today, so I repost:



Yes the time cost is the *same* regardless the depth of the abstraction.



As it is written in TC++PL on page 324:


"Classical hierarchies do tend to couple implementation concerns rather
strongly with the interfaces provided to users. Abstract classes can
help here. Hierarchies of abstract classes provide a clean and powerful
way of expressing concepts without encumbering them with implementation
concerns or significant run-time overheads. After all, a virtual
function call is cheap and independent of the kind of abstraction
barrier it crosses. It costs no more to call a member of an abstract
class than to call any other virtual function."






Regards,

Ioannis Vranos
 
P

pembed2003

E. Robert Tisdale said:
It depends upon the implementation.
But, yes, you are correct for typical implementations.


Actually, in Ioannis Vranos' example,
it appears that p1 and p2 are defined in the same scope
as the respective subsequent invocations of something(void).
In this case, a good optimizing C++ compiler will emit code
to invoke the correct function directly
and will not even consult any virtual function table.

Hi Robert,
Is the "good optimizing C++ compiler will emit code to invoke the
correct function directly" feature a standard behavior? ie, is it
mentioned and allowed by the C++ standard?

Thanks!
 
R

Rob Williscroft

pembed2003 wrote in in
comp.lang.c++:
Hi Robert,
Is the "good optimizing C++ compiler will emit code to invoke the
correct function directly" feature a standard behavior? ie, is it
mentioned and allowed by the C++ standard?

The standard doesn't mention "virtual function table" so there
is no requirment that it be in the programme or that it be
consulted if it is.

The *only* requirment is that the correct function is called,
how that is achived doesn't matter.

Having said that, any programme can be optimised under the "as-if" rule.
i.e. as long as the programme behaves *as-if* the optimisation hadn't
been done then thats fine and dandy.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top