[Week 4] Sample Game Programming Interview Question

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 22172 Views - Last Post: 23 February 2011 - 04:12 PM

#16 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: [Week 4] Sample Game Programming Interview Question

Posted 22 February 2011 - 11:57 AM

Btw, here's an extra question that I don't personally know the answer to. Meyers' singleton although great for single threaded applications is completely unsuited for multi-threaded ones. I guess the alternative is to dynamically allocate the instance (like in Dark_Nexus' example). What methods are there to deallocate the memory if you choose to make a singleton where the instance is allocated dynamically?
Was This Post Helpful? 0
  • +
  • -

#17 Guest_hype261*


Reputation:

Re: [Week 4] Sample Game Programming Interview Question

Posted 22 February 2011 - 01:27 PM

View Postdiego_pmc, on 22 February 2011 - 11:57 AM, said:

Btw, here's an extra question that I don't personally know the answer to. Meyers' singleton although great for single threaded applications is completely unsuited for multi-threaded ones. I guess the alternative is to dynamically allocate the instance (like in Dark_Nexus' example). What methods are there to deallocate the memory if you choose to make a singleton where the instance is allocated dynamically?


Generally speaking to deallocate the memory from a Singleton you have a seperate static method called release. This method just deletes the static instance of the Singleton. This brings about other design decisions like when do you delete your singleton.
Was This Post Helpful? 0

#18 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: [Week 4] Sample Game Programming Interview Question

Posted 22 February 2011 - 03:53 PM

Number 2 is a common question. If it blindsides you; you don't deserve the job. No matter what job you want, you gotta know who you're working for. The best answer is:

Quote

Meh, could be worse.

Was This Post Helpful? 0
  • +
  • -

#19 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 105
  • Joined: 01-July 08

Re: [Week 4] Sample Game Programming Interview Question

Posted 22 February 2011 - 04:44 PM

A singleton is design pattern where only one instance of that object exists during the lifetime of the application.

Singletons are useful for managing tasks that multiple objects need to share.

I've used this approach to reduce calls to binding textures and loading sounds. For example a texture manager would keep track of the currently bound texture so when you request a new texture from the texture manager the manager would know if it needed to bind a new texture or just hand back a handle to the currently bound texture. In this case any object that uses a texture is going to share from the same pool of textures. No need to take up space with multiple copies of the same texture.

Another useful implementation of a singleton would be a logger. Only one object needs to know how to log files and any object that needs to log something would just send it's log message to the logger.


Spoiler


Another great question. This is one of my favorite topics.
Was This Post Helpful? 0
  • +
  • -

#20 Guest_hype261*


Reputation:

Re: [Week 4] Sample Game Programming Interview Question

Posted 23 February 2011 - 07:05 AM

View Postshintetsu_80, on 22 February 2011 - 04:44 PM, said:

A singleton is design pattern where only one instance of that object exists during the lifetime of the application.

Singletons are useful for managing tasks that multiple objects need to share.

I've used this approach to reduce calls to binding textures and loading sounds. For example a texture manager would keep track of the currently bound texture so when you request a new texture from the texture manager the manager would know if it needed to bind a new texture or just hand back a handle to the currently bound texture. In this case any object that uses a texture is going to share from the same pool of textures. No need to take up space with multiple copies of the same texture.

Another useful implementation of a singleton would be a logger. Only one object needs to know how to log files and any object that needs to log something would just send it's log message to the logger.


Spoiler


Another great question. This is one of my favorite topics.



Generally speaking you don't need the assert to ensure that memory was allocated with the new operator. If memory allocation fails then a bad_alloc error will be thrown. You can make the compiler return 0 on memory allocation failure by using std::nothrow
Was This Post Helpful? 0

#21 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,208
  • Joined: 14-February 08

Re: [Week 4] Sample Game Programming Interview Question

Posted 23 February 2011 - 07:29 AM

I really don't see how any problems could arise from having this implementation as a friend:

template<class T>
class Singleton
{
  public:
    static T* Instance()
    {
        // Meyers singleton
        static T obj;
        return &obj;
    }
};



Having it have access to the singleton class it is a friend of is exactly what you want it to do. All it really is is a way of saving you a little implementation and this is definitely a case where I would throw caution to the wind.
Was This Post Helpful? 0
  • +
  • -

#22 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 105
  • Joined: 01-July 08

Re: [Week 4] Sample Game Programming Interview Question

Posted 23 February 2011 - 07:36 AM

View Posthype261, on 23 February 2011 - 07:05 AM, said:

View Postshintetsu_80, on 22 February 2011 - 04:44 PM, said:

A singleton is design pattern where only one instance of that object exists during the lifetime of the application.

Singletons are useful for managing tasks that multiple objects need to share.

I've used this approach to reduce calls to binding textures and loading sounds. For example a texture manager would keep track of the currently bound texture so when you request a new texture from the texture manager the manager would know if it needed to bind a new texture or just hand back a handle to the currently bound texture. In this case any object that uses a texture is going to share from the same pool of textures. No need to take up space with multiple copies of the same texture.

Another useful implementation of a singleton would be a logger. Only one object needs to know how to log files and any object that needs to log something would just send it's log message to the logger.


Spoiler


Another great question. This is one of my favorite topics.



Generally speaking you don't need the assert to ensure that memory was allocated with the new operator. If memory allocation fails then a bad_alloc error will be thrown. You can make the compiler return 0 on memory allocation failure by using std::nothrow



That's true and I did consider removing that from my sample but since I had just pulled it out of one of my old test projects I developed a while back to help myself understand singletons decided to leave it in.

I suppose the real question would be is it wrong or is it just a matter of coding preference?
Was This Post Helpful? 0
  • +
  • -

#23 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [Week 4] Sample Game Programming Interview Question

Posted 23 February 2011 - 09:36 AM

I'm noticing on this thread that everyone is making life more difficult than need be to solve the problem. In a small section of isolated code it is fine to have your templated singletons with friend function, but in reality, in a very large code base like a modern game, you will want to keep things based on first principles. All these extra modifications do is complicate maintenance if employed, since their benefits are negligible to say the least. And I reckon that is why everyone is going round in circle on this thread. :)
Was This Post Helpful? 0
  • +
  • -

#24 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: [Week 4] Sample Game Programming Interview Question

Posted 23 February 2011 - 10:27 AM

Still, I never got an explanation as to why templates are an improvement. I really don't understand this...

Spoiler

This post has been edited by diego_pmc: 23 February 2011 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#25 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [Week 4] Sample Game Programming Interview Question

Posted 23 February 2011 - 10:46 AM

View Postdiego_pmc, on 23 February 2011 - 05:27 PM, said:

Still, I never got an explanation as to why templates are an improvement.

That's because they aren't an improvement to the concept of a singleton. look at it this way, in a game you are only going to implement certain aspects of the game engine (UI, camera, IO, logging, etc.) as a singleton, and since you will be creating a class for them anyway you might as well implement the class as a singleton. :)
Was This Post Helpful? 1
  • +
  • -

#26 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: [Week 4] Sample Game Programming Interview Question

Posted 23 February 2011 - 02:20 PM

Dependency injection is the common and preferred way to avoid mucking with singletons. I don't think DI is very common with C or C++ however. In Java (using Guice) it's just as easy as adding or removing an @Singleton attribute from your class. Other classes which rely on your singleton never know the better.
Was This Post Helpful? 0
  • +
  • -

#27 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: [Week 4] Sample Game Programming Interview Question

Posted 23 February 2011 - 04:12 PM

Singleton can be quite useful. If any of you XNA/C# programmers that are reading this it is relatively painless to make a Singleton class with C#.

Spoiler

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2