Memory

Started by
5 comments, last by CrazyCdn 17 years, 12 months ago
cEngine::cEngine(sEngineStartupParams params) { _params = &(params); } _params is a member of cEngine Ive heard that there is a difference between the memory used by pointers and things made using the new command and non pointer variables. Based on what ive read creating something using the new command puts it in a larger memory area(you can see i dont really know what im talking about). My question is will _params be in an effecient place in memory. Also is this better cEngine::cEngine(sEngineStartupParams params) { _params = new sEngineStartupParams(params); } Thanks :-)
Advertisement
Your second approach is better just make sure to call delete in your destructor.

In your first example _Params is pointing to the address of params. if params is dynamically allocated and later deleted then _Params will now point to invalid memory. If Params is a global or static then this isn't an issue but I still advise the second approach.
Does it store the variable in a different location in memory in the different functions.
yes. data allocated dynamically using new is stored in the heap, local variables are stored on the stack and global variables are alocated at run time and stored in memory as part of your executable's image.
None-of these are more/less efficant in terms of access speed the only difference being that dynamically allocated memory (created with new) requires freeing with delete.
EDIT
Oh and stack memory is destroyed when the function ends so the arguments are destroyed making your fist example an error unless you pass a pointer.
Unless there is a good reason for using new, it is simpler to do it like this:
    class cEngine    {        ...        sEngineStartupParams    m_params;    };    cEngine::cEngine(sEngineStartupParams const & params)    {        m_params = params;     } 
Also, for effiency, it is usually better to pass a const reference rather than a copy to a function (as demonstrated above). Finally, symbols that begin with an underscore (with some exceptions) are reserved for the compiler. You should avoid them.

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Assuming your'e coding in c++, I would just use something like this:

// your startup params:struct sEngineStartupParams{    int _screenwidth;    int _screenheight;...etc....};// your engine classclass cEngine{private:    sEngineStartupParams _params;...etc....};cEngine::cEngine( const sEngineStartupParams& params ){    _params = params; }// you main c++ function entry pointvoid main( void ){    // Create engine init params    sEngineStartupParams params;    params._screenwidth  = 640;    params._screenheight = 480;    // Create engine    cEngine* pEngine = new cEngine( params );    // Main loop    while( !pEngine -> ExitPressed() )     {        pEngine -> Update();    }    // Cleanup    delete pEngine;}


"params" is passed by reference (as opposed to being passed on the stack as you had it). The assignment statement then just makes a copy of the input params within the storage of the engine class. No need to allocate/deallocate etc.
Also - although I allocated cEngine in my example, I would recommend just having the cEngine be a global - in which case you would have an "void cEngine::Initialize( const sEngineStartupParams& params )" function that you call in place of the constructor.

If this doesn't make sense then you might want to read up on the differences between passing function arguments by value / reference, and the differences between pointers and references.

hope this helps,
Steve
Steve Broumley
@guillermoro: Also remember don't name variables with _ or __ infront of them. These are reserved for the compiler, atleast on MS compilers.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

This topic is closed to new replies.

Advertisement