Skip to main content
gamedev.net
🔒 Locked

Pointer problem

Started by jpbb 1 reply 850+ views
Original Post
jpbb
jpbb
Hi, To be honest, I feel bad asking a question like this, but if anyone could help it would be much appreciated. Its late and I'm sure it's something stupid... I have a function to create an instance of my game engine, like so:

bool CreateEngine( Engine *pEngine )
{
    // check if the global engine pointer is already set
    if( g_pEngine )
        return false;

    // create the engine
    try {
        pEngine = new Engine();
    }
    catch( std::bad_alloc )
    {
        return false;
    }
		
    // return success
    return true;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    Engine *pEngine = NULL;

    if( !CreateEngine( pEngine ) )
    {
        // *snip*
    }

    delete pEngine;
}
The call to CreateEngine() returns true, and in the watch window I can see the Engine object is created OK. However, as soon as the function returns the pointer is set back to NULL. Can anyone figure out why? Many thanks.
chowe6685
chowe6685
The pointer is passed by value and thus cannot be changed within the scope of the function and retain those changes outside. You need to either pass a pointer to a pointer or a reference to a pointer to change the pointer in WinMain
jpbb
jpbb
*kicks self*

Thanks, rating++

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.