Jump to content
  • Advertisement
Sign in to follow this  
deviantDarren

C++ / General Questions

This topic is 4158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, i recently recieved a sample set of questions for a job related test. There were a few questions i am unsure of. Any help would be appreciated... Q1. Name something that happens before a c/c++ program enters main()? Q2. Whats the quickest, simplest way to determine in an unsigned integer is a power of two? Q3. Why wouldn't you want to put code for a non-trivial function in a header file? Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
1. Function variables class are declared and headers are included
2. if( x & (x-1) == 0 )
3. This I'm not completely sure about.

Share this post


Link to post
Share on other sites
Quote:
Original post by chubbc
1. Function variables class are declared and headers are included


By that standard, we could also say that the computer is turned on [wink]

Q1 is interesting to determine if the interviewee believes that global and static variables are initialized before main (they are only guaranteed to be initialized before their first use, so side-effects of their initialization should not be relied on). I would definitely answer that the command-line arguments are copied over to argv[][].

Q2 is, in my opinion, silly. It certainly is a "you know it or don't know it trick" of the "google if you don't know it" variety. If the person happens to know the trick, then you can't be sure whether they understand it, and when they don't know it, you can't be sure whether they'd understand it if they knew it. A better version of this question would be:

Does (x & (x-1)) == 0 determine whether x is a power of two? Why or why not?


Q3 is a very good question related to insight about the C++ compiling process. My answers would be reducing the compile time (recompile needed whenever the function is changed), reducing apparent coupling to allow mutual recursion of modules (due to the absence of a module system in C++) and reducing code bloat on compilers silly enough to inline every function definition they encounter.

Share this post


Link to post
Share on other sites
Q1. Static class constructors are called. You can't stick your life on it, but it's a fairly safe bet that this is what they're angling for ;-)

Q3. The reason why is because every time you change the function then any other code which references the function will also need to be recompiled. If you're using precompiled headers the whole project needs to be recompiled.

Share this post


Link to post
Share on other sites
Q1: As far as I can think only the main parameters are initalisised (argv[][]), global variables can be constructed on first-use much like anything else.

Q2:
Quote:
Original post by ToohrVyk
Does (x & (x-1)) == 0 determine whether x is a power of two? Why or why not?


It'll only work for integer values of x where x > 0.

Share this post


Link to post
Share on other sites
Quote:

Original post by ToohrVyk
reducing code bloat on compilers silly enough to inline every function definition they encounter.


But if the function is in a header file, it has to be inline to avoid linker errors, or did I misunderstood you?

Quote:

reducing apparent coupling to allow mutual recursion of modules (due to the absence of a module system in C++)


Could you please explain this a bit more?

Share this post


Link to post
Share on other sites
Quote:
Original post by TheGilb
Q1. Static class constructors are called. You can't stick your life on it, but it's a fairly safe bet that this is what they're angling for ;-)


If I was the one asking the question, I'd be trying to determine if you would actually make that mistake [wink]

Quote:
Original post by Gage64
But if the function is in a header file, it has to be inline to avoid linker errors, or did I misunderstood you?


It can also be static (although this would make matters worse). Also, the inline keyword is only optionally respected by compilers anyway. Still, my remark pertained mostly to member functions.

Quote:
Original post by Gage64
Could you please explain this a bit more?


Certainly. When two headers (which define types) recurse mutually, this clashes with the C and C++ requirement that types be defined before they are instantiated or inherited from, or their members are accessed. This means that, whenever you need to use a type (defined in a header) in this way, you need to include the corresponding header. Which means that if two headers each use the other's type in this way, circular inclusion will occur. The solution is to move one of the implementations to a non-header file.

Share this post


Link to post
Share on other sites
Thank you for the explanation. I'm actually familiar with this, I thought you meant something else because you mentioned that C++ lacks a module system. How would such a system solve this?

Quote:

ORiginal post by ToohrvYk
It can also be static (although this would make matters worse). Also, the inline keyword is only optionally respected by compilers anyway. Still, my remark pertained mostly to member functions.


But again, if the member functions are not inlined won't this cause linker errors? In such a situation, isn't the compiler forced to respect the inline keyword? (Sorry, I'm a bit rusty on these things)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!