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 71 5812
cj wrote: 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?
?????
Puzzled. You aks for help on data structures and 8-queens and are
going to an interview ????
--
Karl Heinz Buchegger kb******@gascad.at
"cj" <cj@yahoo.com> wrote in message
news:fd******************************@news.teranew s.com... 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
Why are you being interviewed for a topic you know nothing about?
Rufus
Yes. And I was hoping that folks here would be more helpful rather than
patronizing.
People are at different levels of their knowledge and at different points in
their lives...
I don't see it embarrassing to ask for help. As I was told in college "there
are no dumb questions"...
Or was gravely I misinformed?..
C++J
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at... cj wrote: 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?
????? Puzzled. You aks for help on data structures and 8-queens and are going to an interview ????
-- Karl Heinz Buchegger kb******@gascad.at
I may or may not be interviewed for this topic, I am just trying to cover as
much ground as possible.
In this economy it's quite hard, as many of us may know.
C++J
"Rufus V. Smith" <no****@nospam.com> wrote in message
news:c7******************************@news.teranew s.com... "cj" <cj@yahoo.com> wrote in message news:fd******************************@news.teranew s.com... 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
Why are you being interviewed for a topic you know nothing about?
Rufus
cj wrote: Yes. And I was hoping that folks here would be more helpful rather than patronizing. People are at different levels of their knowledge and at different points in their lives... I don't see it embarrassing to ask for help. As I was told in college "there are no dumb questions"... Or was gravely I misinformed?..
No.
But your problem seems to be equivalent to:
'What is the difference between all the tools in my toolchest.
I need to know because I am going to an interview for a car-mechanic
job'
Data structures are fundamental in most programming languages. Not
knowing the common ones disqualifies everybody immediatly for a
programming job (in my eyes). I spent 2 semester at university to
study various data structures and algorithms on them.
8-queens per se is more of academic interest, but it is often used
as an entry point in a whole class of recursive algorithms,
known as 'backtracking'. Again: I would expect any serious C or C++
programmer to have programmed it at least once in his studying or
knows how to do it or knows how to find information on the web
about it. There must be thousends of web sites out there dealing
with 8-queens and it is soooo easy to find them ( www.google.com).
--
Karl Heinz Buchegger kb******@gascad.at
Thank you, Karl.
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at... cj wrote: Yes. And I was hoping that folks here would be more helpful rather than patronizing. People are at different levels of their knowledge and at different
points in their lives... I don't see it embarrassing to ask for help. As I was told in college
"there are no dumb questions"... Or was gravely I misinformed?..
No.
But your problem seems to be equivalent to: 'What is the difference between all the tools in my toolchest. I need to know because I am going to an interview for a car-mechanic job'
Data structures are fundamental in most programming languages. Not knowing the common ones disqualifies everybody immediatly for a programming job (in my eyes). I spent 2 semester at university to study various data structures and algorithms on them. 8-queens per se is more of academic interest, but it is often used as an entry point in a whole class of recursive algorithms, known as 'backtracking'. Again: I would expect any serious C or C++ programmer to have programmed it at least once in his studying or knows how to do it or knows how to find information on the web about it. There must be thousends of web sites out there dealing with 8-queens and it is soooo easy to find them (www.google.com).
-- Karl Heinz Buchegger kb******@gascad.at
"cj" <cj@yahoo.com> wrote in message
news:fd******************************@news.teranew s.com... 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?
UNIX is not topical here, so I'll skip that.
About C++, if you are able to do all or most of
the exercises in college-level textbooks, you'll
have a reasonable chance of giving intelligent
answers to interview questions. And at least a
bit of (general programming) knowledge and/or
experience will help.
In short:
Get books. Study. Practice. Practice. Practice.
You can post your code here (with a description of
its purpose) and ask for guidance, advice, review,
and assistance.
Also Google is an excellent way to find study resources.
-Mike
cj wrote: 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[i]=p[i];
}
Regards,
Ioannis Vranos
Ioannis Vranos wrote: Here is one I would ask:
Is the following code guaranteed to be safe and portable?
cat main.cc
#include <string>
#include <vector>
#include <cstddef>
int main(int argc, char* argv[]) {
using namespace std;
class A {
private:
vector<int> array;
string s;
public:
A(void): 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[i] = p[i];
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc ./main
Why would you ask such a question?
What would you expect it to reveal?
There is *no* guarantee that any code will be safe and portable.
Your code appears to comply with the ANSI/ISO C++ standard.
It will port almost everywhere.
Your code has no outputs and no persistent effects
and is "safe" in that sense.
My first suspicion is that you don't really know what you are doing.
I would be reluctant to accept any offer of employment
that you might make.
Perhaps you were simply attempting to be "too clever".
That's a common mistake for both programmers and managers.
If you want to test an applicant's C++ skills,
ask them to write a simple C++ program
or ask them to submit examples of C++ programs that they have written.
Don't test for understanding of subtle features of the language
unless you really need a C++ language lawyer and,
if you hire a C++ language lawyers,
don't expect them to be very productive.
You *will* be disappointed.
E. Robert Tisdale wrote: Ioannis Vranos wrote:
Here is one I would ask:
Is the following code guaranteed to be safe and portable?
> cat main.cc #include <string> #include <vector> #include <cstddef>
int main(int argc, char* argv[]) { using namespace std;
class A { private: vector<int> array; string s;
public: A(void): 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[i] = p[i];
return 0; }
> g++ -Wall -ansi -pedantic -o main main.cc > ./main >
Why would you ask such a question? What would you expect it to reveal?
It would be one of the many, in an effort to determine the depth of ISO
C++ knowledge. That one would be of the difficult ones. There is *no* guarantee that any code will be safe and portable.
There is, for 100% ISO C++ compliant compilers. Others have called you a
troll, but I have seen that you have actual C++ knowledge so let's try
to discuss seriously for once.
Your code appears to comply with the ANSI/ISO C++ standard. It will port almost everywhere. Your code has no outputs and no persistent effects and is "safe" in that sense.
That code could be a part of a bigger program running 24h/24h. My first suspicion is that you don't really know what you are doing. I would be reluctant to accept any offer of employment that you might make.
You know, I have bought an anti-troll spray of a new brand.
Regards,
Ioannis Vranos
E. Robert Tisdale posted: Why would you ask such a question? What would you expect it to reveal?
I believe its intent was to bemuse.
There is *no* guarantee that any code will be safe and portable.
Ofcourse there's a guarantee that code will be safe and portable. That's
what the Standard it all about.
Your code appears to comply with the ANSI/ISO C++ standard. It will port almost everywhere.
It will compile, Yes. But according to the Standard, it contains undefined
behaviour. Crystal clear to me. It will *not* port.
Your code has no outputs and no persistent effects and is "safe" in that sense.
Wrong, it's very very dirty.
My first suspicion is that you don't really know what you are doing.
Wrong, he seems pretty proficient to me.
Perhaps you were simply attempting to be "too clever". That's a common mistake for both programmers and managers.
blah blah blah
If you want to test an applicant's C++ skills, ask them to write a simple C++ program or ask them to submit examples of C++ programs that they have written.
Very good.
But by trying to trick them you also see just how much of an understanding
they have. Consider hiring a car mechanic. Ask them the following question:
You should put deisel in a petrol car:
A) During winter, when the temperature is below zero
B) When the tank is more than half full
If they're an in-any-way-good mechanic, they'll spot the trick and shout,
"NEVER PUT DEISEL IN A PETROL CAR!"
Don't test for understanding of subtle features of the language unless you really need a C++ language lawyer and, if you hire a C++ language lawyers, don't expect them to be very productive. You *will* be disappointed.
Unless ofcourse you ofter them money.
-JKop
Ioannis Vranos wrote: E. Robert Tisdale wrote: There is *no* guarantee that any code will be safe and portable.
There is, for 100% ISO C++ compliant compilers.
That's a lie.
No compiler developer offers such a guarantee.
A agree with Rufus!!!
Do not prepare, just sleep and next day give answers on an interview
quastions.
Valdemar.
"Rufus V. Smith" <no****@nospam.com> сообщил/сообщила в новостях следующее:
news:c7******************************@news.teranew s.com... "cj" <cj@yahoo.com> wrote in message news:fd******************************@news.teranew s.com... 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
Why are you being interviewed for a topic you know nothing about?
Rufus
JKop wrote: It will compile, Yes. But according to the Standard, it contains undefined behaviour. Crystal clear to me. It will *not* port.
Ehehe, I think I must give the answer. Yes it *is* portable. The
standard guarantees that you can treat any object as an array of
unsigned chars of size the result of sizeof() of course (=bytes).
So you can cout the internals of any object! :-)
Also for POD types (which can also be considered as plain char arrays)
it is guaranteed that if you copy them to a char, unsigned char array
you are getting valid objects exact copies of the original.
Consider the following code:
#include <iostream>
#include <cstddef>
int main()
{
using namespace std;
struct test
{
int x[10];
float y[10];
}x={ {0,1,2,3,4,5,6,7,8,9}, {0,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9} };
unsigned char *y=new unsigned char[sizeof(x)];
unsigned char *xp=reinterpret_cast<unsigned char *>(&x);
for(size_t i=0; i<sizeof(x); ++i)
y[i]=xp[i];
test &r=reinterpret_cast<test &>(*y);
cout<<"r.x[]= ";
for(size_t i=0; i<10; ++i)
cout<<r.x[i]<<" ";
cout<<"\nr.y[]";
for(size_t i=0; i<10; ++i)
cout<<r.y[i]<<" ";
cout<<endl;
}
Isn't C++ cool?
Regards,
Ioannis Vranos
E. Robert Tisdale wrote: Ioannis Vranos wrote:
E. Robert Tisdale wrote:
There is *no* guarantee that any code will be safe and portable.
There is, for 100% ISO C++ compliant compilers.
That's a lie. No compiler developer offers such a guarantee.
I still wonder. Since you like C++ why don't you participate seriously
in C++ discussions? If you do not like it, why are you here?
Regards,
Ioannis Vranos
Vladimir Shishkovsky wrote: Do not prepare, just sleep and next day give answers on an interview questions.
And, if you can't sleep,
just stay up all night pounding code and drinking beer. :-)
Ioannis Vranos wrote: E. Robert Tisdale wrote:
Ioannis Vranos wrote:
E. Robert Tisdale wrote:
There is *no* guarantee that any code will be safe and portable.
There is, for 100% ISO C++ compliant compilers.
That's a lie. No compiler developer offers such a guarantee.
I still wonder. Since you like C++, why don't you participate seriously in C++ discussions?
I am serious. No one guarantees that
C++ programs which comply with ANSI/ISO standards are portable --
not compiler developers
and certainly not the ANSI/ISO C++ standard itself.
If you write an ANSI/ISO standard compliant C++ program
and claim that is ports to *any* platform, then *you*
and no one else will be held legally liable if it doesn't.
If you decide to make such a claim,
then you had better test it on the target platform[s] first.
Ioannis Vranos wrote: E. Robert Tisdale wrote:
[who cares what he wrote]
I still wonder. Since you like C++ why don't you participate seriously in C++ discussions? If you do not like it, why are you here?
Because he's a troll. He likes to cause trouble. He posts incorrect or
incomplete information, especially targeting newbies. He changes the
text of quoted posts to make it seem as though people said something
different than they really did. People who disagree with him he labels
as trolls, this includes many of the knowledgable contributors on
comp.lang.c (where he's been more active of late, although he seems to
be switching back over here).
He earned the nickname Trollsdale.
Don't bother attempting to engage him in rational debate, he has no
interest in it.
Brian Rodenborn
Something that calls itself Default User wrote:
[nothing that has to do with C++.]
Go away troll.
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++.
CFG wrote: 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
cj wrote: 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
"Ioannis Vranos" <iv*@guesswh.at.grad.com> skrev i en meddelelse
news:cb***********@ulysses.noc.ntua.gr... cj wrote: 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[i]=p[i]; }
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
"Ioannis Vranos" <iv*@guesswh.at.grad.com> skrev i en meddelelse
news:cb***********@ulysses.noc.ntua.gr... cj wrote:
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
Ioannis Vranos wrote: 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.
E. Robert Tisdale wrote: 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.
Wrong. Next please. :-)
Regards,
Ioannis Vranos
Ioannis Vranos wrote: E. Robert Tisdale wrote:
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.
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
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;
}
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
char jack = reinterpret_cas<char>(monkeys[5]); cout << jack; }
"JKop" <NU**@NULL.NULL> wrote in message
news:UE*****************@news.indigo.ie... JKop posted:
char jack reinterpret_cast<char>(monkeys[5]);
TYPO
char jack = reinterpret_cas<char>(monkeys[5]);
'Tis not your day today... :-)
- Risto -
"E. Robert Tisdale" wrote: 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
Ioannis Vranos <iv*@guesswh.at.grad.com> wrote in message news:<cb***********@ulysses.noc.ntua.gr>... 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!
pembed2003 wrote: 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.
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
pembed2003 wrote: 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.
JKop <NU**@NULL.NULL> wrote in message news:<UE*****************@news.indigo.ie>... 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
char jack = reinterpret_cas<char>(monkeys[5]);
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!
pembed2003 wrote: 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
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<cb*********@nntp1.jpl.nasa.gov>... pembed2003 wrote:
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.
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!
pembed2003 wrote in news:db**************************@posting.google.c om in
comp.lang.c++: 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?
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.
-- http://www.victim-prime.dsl.pipex.com/
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message news:cb7t18$1c19 E. Robert Tisdale wrote: 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.
Wrong. Next please. :-)
What is wrong with Tisdale's answer (btw, is that the preferred short formal
name)?
"pembed2003" <pe********@yahoo.com> wrote in message 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.
I think the virtual table is implemented as an array of pointers to
function. It works well whenever a pointer to a function of any signature
has the same size, which is normally the case. If you have a class as
follows.
Note what follows is a typical implementation, not what the standard
mandates.
class Foo {
virtual ~Foo();
virtual int f(int, int);
virtual double g(int) const;
};
then the virtual table is an array of length 3. Each element in the array
is a function pointer, but each function pointer is a different signature!
Sure, the compiler can do that, but we can't!
Thus, there are 3 C style functions.
void __Foo_destructor(Foo *);
int __Foo_f(Foo *, int, int);
double __Foo_g(const Foo *, int);
and the virtual table is
typedef void (* __FunctionPtr)(); // pointer to non-member function
typedef __FunctionPtr[3] __Foo_vtable_type; // typedef for array of 3
function pointers
__Foo_vtable_type __Foo_vtable = {
(__FunctionPtr)(&Foo_destructor),
(__FunctionPtr)(&Foo_f),
(__FunctionPtr)(&Foo_g)
};
When you say foo->g(3) the compiler knows you mean the element
__Foo_vtable[2] and that it's real signature is double (*)(const Foo *,
int). Thus the call translates to
{
typedef double (* _Foo_vtable2)(const Foo *, int);
const __Foo_vtable_type& __vtable = foo->__vpointer;
_Foo_vtable2 __tmp = (_Foo_vtable2)(__vtable[2]);
(*__tmp)(foo, 3);
}
In message <db**************************@posting.google.com >, pembed2003
<pe********@yahoo.com> writes 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...)
Unless you have virtual inheritance, there's no need for anything so
complicated - it's probably just a linear array, since the offset of
each function's entry in it is known at compile time.
thus takes appr. the same amount of time.
If I answered it incorrectly, can you provide the answer? I want to learn.
--
Richard Herring
Ioannis Vranos wrote: cj wrote: 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?
You should expect a "no", even from someone who doesn't actually know
the answer ;-)
pembed2003 posted: JKop <NU**@NULL.NULL> wrote in message news:<UE*****************@news.indigo.ie>... 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
char jack = reinterpret_cas<char>(monkeys[5]);
> > 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 myself amn't too familiar with casts.
For example, at the moment I only use:
int j = (int)some_float;
I'm not fully certain about :
reinterpret_cast
dynamic_cast
static_cast
I've gone to msdn.microsoft.com looking for an explanation but I find it
hard to understand.
-JKop
JKop wrote: I myself amn't too familiar with casts.
For example, at the moment I only use:
int j = (int)some_float;
I'm not fully certain about:
reinterpret_cast dynamic_cast static_cast
static_cast converts between related types, such as one pointer to
another in the same class hierarchy. The conversion is compile-time checked.
reinterpret_cast handles conversions between unrelated types.
dynamic_cast is a form of run-time checked conversion, and the type of
the object must be *polymorphic*, that is to have virtual functions.
So if in an hierarchy and you want to perform (compile-time checked)
related type conversion use static_cast.
If you want to check at runtime if the types are related use
dynamic_cast provided that the object is polymorphic.
If you want to convert between oranges and potatoes use reinterpret_cast.
Regards,
Ioannis Vranos
JKop wrote: 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; }
Think calmly what are you doing.
#include <iostream>
int main(void)
{
int monkeys[7] = { 3, 4 ,3, 2 ,2 ,34, 23};
char jack=monkeys[5];
std::cout << jack;
}
Of course this will work only for the values of int that can be
represented in char.
There are people who want to mark that the above is performing an ugly
operation (which it does, implicit type conversions are a C inheritance)
you can use static_cast:
char jack=static_cast<char>(monkeys[5]);
Regards,
Ioannis Vranos
Ioannis Vranos posted: JKop wrote:
I myself amn't too familiar with casts.
For example, at the moment I only use:
int j = (int)some_float;
I'm not fully certain about:
reinterpret_cast dynamic_cast static_cast static_cast converts between related types, such as one pointer to another in the same class hierarchy. The conversion is compile-time checked.
reinterpret_cast handles conversions between unrelated types.
dynamic_cast is a form of run-time checked conversion, and the type of the object must be *polymorphic*, that is to have virtual functions.
So if in an hierarchy and you want to perform (compile-time checked) related type conversion use static_cast.
If you want to check at runtime if the types are related use dynamic_cast provided that the object is polymorphic.
If you want to convert between oranges and potatoes use reinterpret_cast. Regards,
Ioannis Vranos
Is reinterpret_cast stupid, by which I mean does it do any processing at
all? For example, take the following:
unsigned char jk;
reinterpret_cast<unsigned long&>(jk) = 4000000000UL;
Will it do any truncation or anything? Or will it just try to stuff a
possibly 32-Bit value into a possibly 8-Bit space, effectively writing into
unallocated memory? If so...
Then what exactly is a "variable". Is it essentially just a chuck of
allocated memory of the size of the type which is used to define it. In my
previous example, does the compiler just take j as an address, and then just
work with in a way which is defined by the type which is used to
access/alter it?
Imagine there was no such thing as unions in C++, would the following code
work. Firstly, here's the union form of it:
union {
double double_data;
int int_data;
char char_data;
unsigned long unsigned_long_data;
void* void_pointer_data;
float* float_pointer_data;
}
//Now some funky code:
double recpoo;
reinterpret_cast<float*&>(recpoo) = &some_global_float;
reinterpret_cast<unsigned long&>(recpoo) = 4000000000UL;
reinterpret_cast<char&>(recpoo) = 'g';
Next topic, what kind of cast is the following:
int j;
void* t = (void*)&j;
Thanks for the help!
-JKop
Ioannis Vranos posted: JKop wrote:
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; } Think calmly what are you doing.
#include <iostream>
int main(void) { int monkeys[7] = { 3, 4 ,3, 2 ,2 ,34, 23};
char jack=monkeys[5];
std::cout << jack; }
Of course this will work only for the values of int that can be represented in char.
There are people who want to mark that the above is performing an ugly operation (which it does, implicit type conversions are a C inheritance) you can use static_cast:
char jack=static_cast<char>(monkeys[5]);
Opps, that wasn't my intention, this was my intention:
int main(void)
{
int monkeys[7] = { 3, 4 ,3, 2 ,2 ,34, 23};
std::cout << reinterpret_cast<char&>(monkeys[5]);
}
-JKop
JKop wrote: Is reinterpret_cast stupid, by which I mean does it do any processing at all? For example, take the following:
unsigned char jk;
reinterpret_cast<unsigned long&>(jk) = 4000000000UL;
Reinterpret_cast performs a value conversion. It doesn't modify anything
on the implementation of jk. So the above has essentially no effect. It
is as if you had written
reinterpret_cast<unsigned long&>(jk);
Imagine there was no such thing as unions in C++, would the following code work. Firstly, here's the union form of it:
union {
double double_data; int int_data; char char_data; unsigned long unsigned_long_data; void* void_pointer_data; float* float_pointer_data;
}
//Now some funky code:
double recpoo;
reinterpret_cast<float*&>(recpoo) = &some_global_float;
reinterpret_cast<unsigned long&>(recpoo) = 4000000000UL;
reinterpret_cast<char&>(recpoo) = 'g';
No, reinterpret_cast has no effect in the above.
Next topic, what kind of cast is the following:
int j;
void* t = (void*)&j;
C-style. The most unsafe.
Regards,
Ioannis Vranos
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cb**********@nntp1.jpl.nasa.gov... Ioannis Vranos wrote:
E. Robert Tisdale wrote:
Ioannis Vranos wrote:
E. Robert Tisdale wrote:
> There is *no* guarantee that any code will be safe and portable.
There is, for 100% ISO C++ compliant compilers.
That's a lie. No compiler developer offers such a guarantee. I still wonder. Since you like C++, why don't you participate seriously in C++ discussions?
I am serious. No one guarantees that C++ programs which comply with ANSI/ISO standards are portable -- not compiler developers and certainly not the ANSI/ISO C++ standard itself.
Yes, the standard does guarantee that compliant code
is portable to a compliant implementation. Of course
compliant implementations do not exist for all platforms.
If you write an ANSI/ISO standard compliant C++ program and claim that is ports to *any* platform,
"Any platform" would be a an impossible to make guarantee.
"Any compliant implementation" is entirely possible.
then *you* and no one else will be held legally liable if it doesn't. If you decide to make such a claim,
I've never seen such a claim.
then you had better test it on the target platform[s] first.
Of course testing is always a good idea.
But 'porting' in the context of standard C++ means
among implementations, not 'platforms'.
-Mike This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: softwareengineer2006 |
last post by:
All Interview Questions And Answers
10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP)
I have listed over 10000 interview questions asked
in interview/placement test papers for...
|
by: Jobs |
last post by:
Download the JAVA , .NET and SQL Server interview sheet and rate
yourself. This will help you judge yourself are you really worth of
attending interviews. If you own a company best way to judge if...
|
by: Jobs |
last post by:
Download the JAVA , .NET and SQL Server interview with answers
Download the JAVA , .NET and SQL Server interview sheet and rate
yourself. This will help you judge yourself are you really worth of...
|
by: connectrajesh |
last post by:
INTERVIEWINFO.NET
http://www.interviewinfo.net
FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES
NO ADVERTISEMENT
|
by: freepdfforjobs |
last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions
http://www.questpond.com/SampleInterviewQuestionBook.zip
Download the JAVA , .NET and SQL Server interview sheet and rate...
|
by: freesoftwarepdfs |
last post by:
Ultimate list of Interview question website.....Do not miss it
http://www.questpond.com
http://msdotnetsupport.blogspot.com/2007/01/net-interview-questions-by-dutt-part-2.html...
|
by: ramu |
last post by:
C# Interview Questions and Answers8
http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html
C# Interview Questions and Answers7...
|
by: reema |
last post by:
EJB Interview Questions
http://interviewdoor.com/technical/EJB-Interview-Questions.htm
CSS Interview Questions
http://interviewdoor.com/technical/CSS-Interview-Questions.htm
C Interview Questions...
|
by: reema |
last post by:
EJB Interview Questions
http://interviewdoor.com/technical/EJB-Interview-Questions.htm
CSS Interview Questions
http://interviewdoor.com/technical/CSS-Interview-Questions.htm
C Interview Questions...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |