Jump to content
  • Advertisement
Sign in to follow this  
lem77

C++ Objects or pointers to them

This topic is 4765 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

Hi all, another noob question by me ;) I´m generally wondering what is the difference between using an Object "directly" or using an pointer to the object. E.g. what is the difference in using a) std::map<std::string, CModel> b) std::map<std::string, *CModel> Is it only that with pointer I´m allocating memory with "new"? Thanks guys

Share this post


Link to post
Share on other sites
Advertisement
In the pointer version the object itself is stored in the map and with a pointer, only the adress to the object.
You can pass a object that was allocated with new to the non-pointer version too.
The map will store a copy of it.

For large objects the pointer version is the better one, for small objects, like ints the non-pointer version is the better one.

If you're using the pointer version, You could use boost::shared_pointer (www.boost.org) so you don't have to think about deleting your objects later manually.

Share this post


Link to post
Share on other sites
Well one of the key differences, besides from how they are allocated is how you store them, and pass them between objects. In the example you posted, if CModel was very large, like a kilobyte in size - unlikely, but this is just an example - then adding objects to the map would be quite inefficient, as for each CModel a full kilobyte has to be copied. In the later example only 4 bytes has to be moved into the map.

Another major difference is that the object stored in the first map is a copy of the first object, whereas the second is a pointer to the original. This is important if the destructor of CModel releases any memory, or performs other similar activities.

Share this post


Link to post
Share on other sites
The main difference comes into play when you use objects in function parameters or return values. When you return a pointer or pass a pointer to an object, you are only passing 4 bytes. If you pass or return an unreferenced object, you create a new copy of the object which causes that objects construction and copy methods to be called. So in short, it makes your program faster to us pointers or referenced objects as method parameters and return types.

Share this post


Link to post
Share on other sites
thx dudes

Quote:
Original post by stro
In the later example only 4 bytes has to be moved into the map.


I understand that. Since I´m trying to followe manager-pattern wherever I can, I don´t have trouble with function arguments, since I try to avoid them (I keep it simple, since I am a noob :D )

Share this post


Link to post
Share on other sites
There is an issue when you insert things into the map. If you insert a pointer whoever does the insertion is still responsible for memory management. That means something like this will cause problems.



std::map<int, *CModel> myMap;

void storeModel(void)
{
CModel cm; //Create a cm on the stack.
myMap.insert(1, &cm); //Inserting the address of cm.
} //Function returns and cm is destoyed. What does the map entry point to?



Share this post


Link to post
Share on other sites
I had troubles there before. So whats the matter?
How/Where should I create cm ?

Share this post


Link to post
Share on other sites
Quote:
Original post by lem77
I had troubles there before. So whats the matter?
How/Where should I create cm ?



You could do this:


std::map&lt;int, CModel*&gt; myMap;

void storeModel(void)
{
CModel *pCM = new CModel; //Create a cm in the HEAP.
myMap.insert(1, pCM); //Inserting a pointer to cm.
}



Then, when your application is ending, or when you wish to clear out myMap, you'll need a function to iterate through the map and delete the pointers to CModels. That way, you don't have memory leaks.

If you don't want to use iterators, do something like this:


CModel *pModel = NULL;
for( unsigned int i = 0; i &lt; myMap.size(); ++i )
{
pModel = myMap[ i ];
delete pModel;
pModel = NULL;
}



Or, if you like iterators, do something like this:


CModel *pModel = NULL;
std::map&lt;int, CModel*&gt;::iterator itt;
for( itt = myMap.begin(); itt != myMap.end(); ++itt )
{
pModel = (*itt).second;
delete pModel;
pModel = NULL;
}

Share this post


Link to post
Share on other sites
As an aside you might want to think about using std::auto_ptr or std::tr1::shared_ptr<> to try to avoid memory leakage. boost also provide an implementation of the sharded_ptr<> template.

If you are concerned about memory leakage try to use these.

You might still want to avoid dangling pointers so seting the pointer in your map to 0 or null would still be required after the object being pointed to was released back to the heap.

:)

Oh. I was told (on gamedev) that it is better to use std::pair<> to insert into maps:


std::map<int, CModel*> myMap;
typedef std::pair<int, CModel*> myMapPair;

void storeModel(void)
{
CModel *pCM = new CModel; //Create a cm in the HEAP.
myMap.insert(myMapPair(1, pCM)); //Inserting a pointer to cm.
}




Hope this helpsa little.

Share this post


Link to post
Share on other sites
Heyho, thx guys.

I dont want to use boost, as it is somewhat overhead. Maybe i try (Paul Nettles?) memory_manager, when my project grows bigger ...

What is this syntax???
std::map<int, CModel*> myMap;
Never saw it before.

I want to make a ModelManager (as u guys guessed :D)

If in the manager I have a store-function like garyfletcher/sanity asylum mentioned, when should i delete the pointer and where?
Do I need a release-function where I iterate the pointers in the map and destroy them?
Do I understand it the right way?

Thanx again, great help!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!