# Unity Alternative for std::vector/STL?

## Recommended Posts

Hi everybody, A few months ago in one of my projects I used the std::vector class. I restarted my project and am working on small executables / 64K apps. But because of this - I lost the opportunity to use the std::vector class. Is there a possibility to include it in my projects? I like this class. It is very easy to use, I think (okay - the parts of it I need). So...Any alternatives for std::vector/STL??? thx! cu

##### Share on other sites
Well, if your data is static, you could just use a standard c-array. If you do need the flexibility of a vector, it is pretty easy to make a simple one yourself.

The class contains a templated pointer and the size of the array. Whenever you need more space in the array, copy its contents to another array, re-new the original array to twice the size, then copy the data back. Add in a few functions to push_back and overload the [] operator and you are set...

##### Share on other sites
What makes you think the standard vector takes up more space than any alternative?

##### Share on other sites
I agree with Sr_Guapo, if you're making a tiny exe, then you'll most likely want to write your own vector class so you have as much control over it as possible. An array would probably result in less code, so long as it doesn't need resized often.

##### Share on other sites
You should just write your own vector class, in most cases it is just a linked list. If you need any advice on how to make it just let us know.

theTroll

##### Share on other sites
Quote:
 Original post by TheTrollYou should just write your own vector class, in most cases it is just a linked list. If you need any advice on how to make it just let us know.

He shouldn't write a vector class as a linked list, he should write a linkedlist (or list/slist as C++ calls it) class as a linked list. Also I agree if you need a small executable, then STL shouldn't be used. Your own, non-templated (templates, in most cases create code for every template class), class might be the best idea.

##### Share on other sites
Quote:
 Original post by BregmaWhat makes you think the standard vector takes up more space than any alternative?

He doesn't mean overhead from the stl-vector implementation,
but space overhead from linking with the stl - which will make
his 64kb demo bigger (eg stlport.lib is 950 kb big).

And it shouldn't be too difficult to make your own custom
vector-template which doesn't depend on anything... Again, usually
I would advice against custom vector/list/... implementation, but
for a 64kb demo I can see whe he wants to do it that way.

##### Share on other sites
The reason I said write a "vector" class is so that he would write a class using the same naming convntion and functions that he is used to.

theTroll

##### Share on other sites
Quote:
Original post by CTar
Quote:
 Original post by TheTrollYou should just write your own vector class, in most cases it is just a linked list. If you need any advice on how to make it just let us know.

He shouldn't write a vector class as a linked list, he should write a linkedlist (or list/slist as C++ calls it) class as a linked list. Also I agree if you need a small executable, then STL shouldn't be used. Your own, non-templated (templates, in most cases create code for every template class), class might be the best idea.

I've read about at least one implementation of the vector class which is incredibly efficient, and rather clever in my book, regarding code size. It specializes std::vector<void*>, I believe, and implements just about all of the code there. Then for every other type of std::vector, it inherits from std::vector<void*>, and the only additional code it needs to add is a few functions that contain a reinterpret_cast<>() call, which takes very little space (possibly no space?) code-wise.

If you do write your own vector class, it wouldn't hurt to look at the code for an already implemented version. You might find some useful things in there.

##### Share on other sites
Quote:
 Original post by TheTrollYou should just write your own vector class, in most cases it is just a linked list. If you need any advice on how to make it just let us know.theTroll

Its my understanding that in all cases its continous memory, that's part of the spec. You can't legally implement a vector as a linked list.

Ahh, I see you clarified your point. I'll accept that logic:)

Cheers
Chris

##### Share on other sites
Quote:
 Original post by TheTrollThe reason I said write a "vector" class is so that he would write a class using the same naming convntion and functions that he is used to. theTroll

When looking for a class with the same performance characteristics as a linked list he should be used to using std::list or std::slist. Well we should stop discussing this, since it's going off-topic I think.

##### Share on other sites
Writing 64k demos is predominantly about minimizing code size. In these conditions using templates is sketchy because it expands binary size very quickly. It may actually be wiser to use type-unsafe void* based containers if you want to fit inside that 64 kilobyte cage.

##### Share on other sites
Quote:
 Original post by Kitt3nHe doesn't mean overhead from the stl-vector implementation,but space overhead from linking with the stl - which will makehis 64kb demo bigger (eg stlport.lib is 950 kb big).

When you link to a static library, you don't typically merge the whole library with your program, but only those bits you have used. Furthermore, it is my understanding that stlport.lib only contains precompiled iostream class instanciations (for char and wchar_t); std::vector is still pulled in and compiled from the source that is in the <vector> header.

##### Share on other sites
Quote:
 Original post by TheTrollYou should just write your own vector class, in most cases it is just a linked list.

ROFL a STL vector is NEVER implemented as a linked list. That's just preposterous.

##### Share on other sites
Quote:
 Original post by TheTrollYou should just write your own vector class, in most cases it is just a linked list. If you need any advice on how to make it just let us know.theTroll

If you write vector classes as linked lists. How then do you solve the O(1) access time that vectors are supposed to boast...

##### Share on other sites
not even 'supposed to boast', more like 'required to have'.

##### Share on other sites
Avoiding STL containers because they could be bloated is a typical premature optimization.
It should be easy to write some code using std::vector and inspect the size of what std::vector specializations compile to; in a 64k demo this kind of analysis is likely to be required anyway.

[Edited by - LorenzoGatti on May 2, 2006 11:59:37 AM]

##### Share on other sites
It's not the bloat of vector it's from the fact, that you have duplicated code fpr every instanziation. Therefore it is better to use a generic void* vector and then cast it to the right type (this could be in a template, as mentioned some posts before).

##### Share on other sites
Quote:
 Original post by Anonymous PosterIt's not the bloat of vector it's from the fact, that you have duplicated code fpr every instanziation. Therefore it is better to use a generic void* vector and then cast it to the right type (this could be in a template, as mentioned some posts before).

You don't think partial specialization is good enough? Could you post your profiling results?

##### Share on other sites
Quote:
Original post by Bregma
Quote:
 Original post by Anonymous PosterIt's not the bloat of vector it's from the fact, that you have duplicated code fpr every instanziation. Therefore it is better to use a generic void* vector and then cast it to the right type (this could be in a template, as mentioned some posts before).

You don't think partial specialization is good enough? Could you post your profiling results?

The point is that even partial specialisation creates more code than using a generic void*. For every type he wants to use, the preprocessor will generate the code to handle that type. That's what templates do, and as remarkably useful as they are, they do increase code-size. It's not about speed (unless you meant code-size when talking about "profiling", in which case, you know that already).

##### Share on other sites
Quote:
 Original post by BregmaCould you post your profiling results?

I tried a simple code, where the compiler (VS 2005) most likely have optimized some stuff away. But with the following code:
#include <vector>#include <iostream>template<typename T>void print(const std::vector<T>& p){	for( std::vector<T>::const_iterator iter = p.begin();		p.end() != iter;		++iter)	{		std::cout << (*iter) << std::endl;	}}int main(){#ifdef USE_VOID_PTR	std::vector<void*> a;	std::vector<void*> b;	std::vector<void*> c;	std::vector<void*> d;	std::vector<void*> e;	std::vector<void*> f;#else	std::vector<char> a;	std::vector<short> b;	std::vector<int> c;	std::vector<long> d;	std::vector<float> e;	std::vector<double> f;#endif	for( unsigned int i = 0;i<10;++i)	{		a.push_back( 0 );		b.push_back( 0 );		c.push_back( 0 );		d.push_back( 0 );		e.push_back( 0 );		f.push_back( 0 );	}	print( a );	print( b );	print( c );	print( d );	print( e );	print( f );		return 0;}

If USE_VOID_PTR is defined the generated executable (release mode, optimize for size) is 9728 bytes, but if USE_VOID_PTR isn't defined the generated code is 14848 bytes. So the executable growed with 66% or 5120 bytes.

EDIT: Using my own, simple vector, I was able to get the same executable down to 6656 (no other changes than switching the vector type and removing the include).

[Edited by - CTar on May 2, 2006 8:34:28 AM]

##### Share on other sites
Quote:
 Original post by CTarIf USE_VOID_PTR is defined the generated executable (release mode, optimize for size) is 9728 bytes, but if USE_VOID_PTR isn't defined the generated code is 14848 bytes. So the executable growed with 66% or 5120 bytes.EDIT: Using my own, simple vector, I was able to get the same executable down to 6656 (no other changes than switching the vector type and removing the include).

The test isn't fair: it instantiates only print<void*>, not the six numeric print<> specializations (which are also likely to pull in something specific from <iostream>).
You also dodge the casts and dereferencing of the void* pointers that are required to actually access the objects.

##### Share on other sites
Really, it looks like he's using them to me!

Cheers
Chris

##### Share on other sites
The whole point is kinda moot anyway, 64K demos should be optimized for executable size so linking unnecessary code is out of the question. And when using libraries your executable *will* be contaminated one way or the other, unless you delve into the disassembly and make sure it doesn't. On the other hand there isn't any advantage for resizable arrays either, as the 64k limit doesn't involve the use of memory at runtime, so you could just grab as much memory as you need and access it directly.

Also, C++ requires some bodily libs of itself, so perhaps it isn't the best language to do 64K demos in. There is hardly a way around C or ultimately ASM, I guess, if you'd like to add some data to your file too. Search for a library named libctiny, it offers a minimal C runtime which results in really small .exe's, at the cost of functionality of course. You could link to it from your C++ compiler too.

##### Share on other sites
Quote:
 Original post by chollida1Really, it looks like he's using them to me!

Quote:
 The test isn't fair: it instantiates only print, not the six numeric print<> specializations

Then no hes not using them, LorenzoGatti is right, the test is unfair.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627700
• Total Posts
2978690
• ### Similar Content

• Hi all!
We are looking for a C# programmer for our 2D Action RPG titled Adavia, made in Unity.
The game itself is akin to Legend of Zelda: Link to the Past, though we're also adding in traditional RPG elements such as Character Creation.
This is more of a hobby than anything commercial, if it somehow does manage to go commercially, all revenue will be split equally among the team.
If you're interested, we ask that you be comfortable with:
Coding A.I's for enemies and NPCs. Working with GUI's. Communicating regularly with the team via Skype (text only). If you have any questions or would like to apply, please contact me at nathan.jenkins1012@gmail.com

• You are the commander of a special forces squadron. You were given a task that appeared simple at first glance - to check for suspicious activity in the building of an abandoned psychiatric hospital. But you could not even imagine what you will actually have to face.

• You find yourself in an abandoned place full of mutants in the dead of night, and have to kill waves of monsters with a different kind of weapon. The main goal is to survive through the night.
Controls:
WASD – Walk
Shift – Run
Mouse1 - Attack
Space - Jump
Scroll Down – Change weapon
Esc - Exit, pause

• "Lost Signal" agency investigates paranormal events from all around the world. You are one of the agents who participates in the research of various artifacts. Investigate paranormal activities in this 3D game which has great action and elaborate 3D graphics.
Interesting quests and creepy monsters await you!

• By trjh2k2
I've never really been a "Unity guy", since all of my game-dev learning happened in C++, and in other engines, but I recently discovered the "complete projects" section in the asset store.  It's full up on projects you can buy that are billed as "ready to customize and release", with full ad integration.  Some of them claim to be for educational purposes, but why would you include a complete, polished, full featured game with ads as an educational example?
This leads me to the question of why this goes by unchallenged?  Does Unity and the environment of the Unity Store actively encourage this style of game development?  Is the problem of asset flipping our own fault?  I don't mean this as a "we should make Unity shut this down" kind of thread, but rather just to examine whether or not the environment of being able to just buy whole games or pieces of games is something that damages the industry.  I get why Unity would allow it, and I'm sure it's a working business model for some people- and maybe some people DO actually just use these to learn from, but I'm not that naive as to think that there aren't people who recognize this as one of the shortest paths to putting a game on the market so they can cash in.
Thoughts?

• 20
• 14
• 12
• 10
• 12