STL containers that contain pointers

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
Pangamini
Gremlin
Posts: 179
Joined: Fri Sep 17, 2010 2:21 pm
Location: Kosice, Slovakia
x 3

STL containers that contain pointers

Post by Pangamini »

Hello there
I am using many stl containers in my projects, most of them only contain pointers but to different class objects. For example

Code: Select all

std::vector<Prop*>
std::vector<Shape*>
std::map<string,Prop*>
std::map<string,Player*>
In the example, how many templates are compiled? 2 or 4?
I am using VS2008
Ancient Greeks did have a culture, but they didn't have gasoline.
...and we have gasoline.
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94

Re: STL containers that contain pointers

Post by tod »

4, one template for each type.
Is anybody offering more? :D

On a side note, I seem to remember it was possible to see the output of the preprocessor, and that will definitely answer your question. But I don't remember how, or in which IDE? :?
User avatar
Pangamini
Gremlin
Posts: 179
Joined: Fri Sep 17, 2010 2:21 pm
Location: Kosice, Slovakia
x 3

Re: STL containers that contain pointers

Post by Pangamini »

Is there any way how to reduce it? Since the output code will be exactly the same...
Ancient Greeks did have a culture, but they didn't have gasoline.
...and we have gasoline.
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58

Re: STL containers that contain pointers

Post by CABAListic »

No, there isn't. Your only alternative would be to use one container for void* pointers and cast back and forth to your actual pointer types. But templates exist specifically to avoid such things.
User avatar
Pangamini
Gremlin
Posts: 179
Joined: Fri Sep 17, 2010 2:21 pm
Location: Kosice, Slovakia
x 3

Re: STL containers that contain pointers

Post by Pangamini »

Well, maybe i could create a wrap around the container, a template object that contains a shared void* container and does the casting... a little late for that, though :-)
Ancient Greeks did have a culture, but they didn't have gasoline.
...and we have gasoline.
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58

Re: STL containers that contain pointers

Post by CABAListic »

That would be pointless, because your wrapper would again be instantiated for all your pointer types.

Why is this so important, anyway? Yes, usage of the STL will increase your executable size, but is that really a serious issue for you?
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94

Re: STL containers that contain pointers

Post by tod »

Templates get expanded by the preprocessor. This means vector<int> and vector<float> will actually generate two classes that offer the vector functionality, one for int and one for float. Of course you could do this yourself :D But with more work, less speed and more bugs.
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58

Re: STL containers that contain pointers

Post by CABAListic »

The preprocessor has nothing to do with templates; it doesn't have enough information to do such a sort of thing. Templates are handled by the compiler itself.
User avatar
Pangamini
Gremlin
Posts: 179
Joined: Fri Sep 17, 2010 2:21 pm
Location: Kosice, Slovakia
x 3

Re: STL containers that contain pointers

Post by Pangamini »

tod wrote:Templates get expanded by the preprocessor. This means vector<int> and vector<float> will actually generate two classes that offer the vector functionality, one for int and one for float. Of course you could do this yourself :D But with more work, less speed and more bugs.

Yeah that's nice. But vector<int*> and vector<float*> are actually the same as vector<SceneNode*> and vector<void*>
Ancient Greeks did have a culture, but they didn't have gasoline.
...and we have gasoline.
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94

Re: STL containers that contain pointers

Post by tod »

I guess you could do something like this:

Code: Select all

vector<int *> nodes;
nodes.push_back((int*) mySceneNodePtr);
if you really, really want it.
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94

Re: STL containers that contain pointers

Post by tod »

The preprocessor has nothing to do with templates; it doesn't have enough information to do such a sort of thing. Templates are handled by the compiler itself.
Got it.
SillySalmon
Gnoblar
Posts: 9
Joined: Sat Sep 17, 2011 4:12 am

Re: STL containers that contain pointers

Post by SillySalmon »

Pangamini wrote:Hello there
I am using many stl containers in my projects, most of them only contain pointers but to different class objects. For example

Code: Select all

std::vector<Prop*>
std::vector<Shape*>
std::map<string,Prop*>
std::map<string,Player*>
In the example, how many templates are compiled? 2 or 4?
I am using VS2008
This is a very good question. You can use the debugger to find out.
Create some code like this:
std::map<int,float> foo;
std::map<int,char*bar> baz;

foo.insert(std::pair<int,float>(1,3.4f));
baz.insert(std::pair<int,char*>(4,"hello world!"));

Then use the debugger to step into each insert function as far as you can go. If you're lucky you might find that when creating the items in the map (creating tree nodes, allocating memory) it might drill down to some shared code rather than duplicating two classes. You might have to drill down quite a long way to find any common code. But if there is common shared code then at least you know that it isn't duplicating the whole templated class twice. Eventually you'll get down to things like malloc, you need not step into those runtime libs any further since that is about as far as it is useful to drill down.
User avatar
betajaen
OGRE Moderator
OGRE Moderator
Posts: 3447
Joined: Mon Jul 18, 2005 4:15 pm
Location: Wales, UK
x 58

Re: STL containers that contain pointers

Post by betajaen »

Personally, it seems rather silly to me.

Code: Select all

std::vector<void*> v;
v.push_back((void*) new int());
v.push_back((void*) new class_name());
How are you going to know what types of pointers they are? Obviously some sort of identification system; from a simple struct with an enum/integer to represent the type and the pointer, to a well designed shared-ptr auto casting template based system.

Either way it seems to me you'll just use the same amount of memory than just having two seperate vectors.
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: STL containers that contain pointers

Post by areay »

Pangamini wrote:Is there any way how to reduce it? Since the output code will be exactly the same...
Why would you want to reduce it? I can't imagine it would be causing you any problems whatsoever.

Also, the output code is not the same. Each of the classes generated by the compiler will internally be using a unique types for array storage, functions and more. This is the whole point of the STL. So useful they made it part of the C++ standard. :D
User avatar
syedhs
Silver Sponsor
Silver Sponsor
Posts: 2703
Joined: Mon Aug 29, 2005 3:24 pm
Location: Kuala Lumpur, Malaysia
x 51

Re: STL containers that contain pointers

Post by syedhs »

betajaen wrote:Personally, it seems rather silly to me.

Code: Select all

std::vector<void*> v;
v.push_back((void*) new int());
v.push_back((void*) new class_name());
I think what he wants is same STL code for all stl pointers ie std::vector<whateverpointer> will point to one source code since the logic thinking is pointer is always 32 or 64 bit.
So some code can be saved if variables can be declared like below:-

Code: Select all

std::vector<int*> a;
std::vector<myclass*> b;
std::vector<float*> c;
All above will resolve into one final class instead of 3 different classes (which he hopes).
A willow deeply scarred, somebody's broken heart
And a washed-out dream
They follow the pattern of the wind, ya' see
Cause they got no place to be
That's why I'm starting with me
User avatar
betajaen
OGRE Moderator
OGRE Moderator
Posts: 3447
Joined: Mon Jul 18, 2005 4:15 pm
Location: Wales, UK
x 58

Re: STL containers that contain pointers

Post by betajaen »

Oh, I understand.

But just because int* and className* have the same size, and essentially are the same thing. It doesn't mean that the thing that those pointers are pointing to are. My point was that you would have to end up writing a system to contain and handle the type of pointer and casting into the correct type. You'd end up using the same amount of resources (if not more) and memory implementing a system, rather than just having three separate vectors.
User avatar
Pangamini
Gremlin
Posts: 179
Joined: Fri Sep 17, 2010 2:21 pm
Location: Kosice, Slovakia
x 3

Re: STL containers that contain pointers

Post by Pangamini »

But template types are recognized statically... so is the size of objects.
Ancient Greeks did have a culture, but they didn't have gasoline.
...and we have gasoline.
Czar
Kobold
Posts: 35
Joined: Sat Nov 05, 2011 12:18 am

Re: STL containers that contain pointers

Post by Czar »

You could create a special base class that all your classes inherit from, and have your containers with pointers to this base class. Then use dynamic_cast to retreive the object and make sure you are getting the right one (because this stuff is just asking for pointer errors). This exercise is academic and will gain you nothing of value.