c++ - std::vector<T*> vs std::vector<T> for 64-bit T -


in 3d geometry engine code read that:

std::vector<ga_offset*> seems wrong, ga_offset merely 64-bit integer. std::vector<ga_offset> better.

where ga_offset aka long int a, possibly, non-contiguous offset element in geometry structure. ga_offsets remain constant if element prior deleted, invalidated if element inserted prior it, or if element list defragmented.

in short c++ experience i've learned references , pointers better passing values. i'm little confused here.

why storing values in std::vector better pointer? difference 64-bit int makes?

std::vector<t> make sense if t 64-bit integer, memory layout cache-friendly contiguous array of ts:

+---------+  |t|t|...|t| +---------+ 

on other hand, std::vector<t*> more complex data structure, store pointers contiguously; items (integers) pointed not contiguous in memory:

+--------------+  |t* | t*|...|t*| +--------------+  |   |       |  \-> \->     \-> 

so, vector<t*> more inefficient , less cache-friendly vector<t> simple integer t.

i suggest using vector<t> 64-bit integer t. why need additional level of indirection of t*?

moreover, pay attention when storing raw pointers in stl containers. raw observing pointers fine; raw owning pointers source of "leaktrocities", , should consider storing smart owning pointers instead (e.g. std::vector<std::unique_ptr<t>>). but, again, doesn't make sense simple 64-bit integer type.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -