Hi all what is better(Talking about memory and performance spped ) for store the float vertex using std::vector vrtx<float> or use a raw array / like float vrtx[]
thanks in advance
Hi all what is better(Talking about memory and performance spped ) for store the float vertex using std::vector vrtx<float> or use a raw array / like float vrtx[]
thanks in advance
std::vector, under the covers, is just a managed array -- it has precisely the same layout in memory as a native array, or a contiguous block of dynamically-allocated memory, with the benefit that you neither have to allocate it statically (native array) or remember to delete it (dynamically-allocated memory). In C++ 11, std::vector (and other containers) also support move semantics which, properly afforded and called upon, eliminates redundant linear copy operations which a raw array cannot avoid AFAIK (perhaps as a particularly aggressive compiler optimization, given particularly formatted source code) -- A real-world example of this would be a function that returns the contents of a locally-declared array (which will need to be copied by the calling code), vs the same function which returns a locally-declared std::vector, which will simply move the std::vector internals between the locally-defined vector and the vector in the calling code, provided that either the return type is an rvalue reference (&&) itself, or std::move is invoked at the call-site.
In general, always prefer the *correct* standard container over native arrays and dynamic memory allocation -- there is some skill in choosing the right container based on your usage, and in configuring your build to not leave performance on the table (e.g. disable checked iterators) but the standard (or some other well-worn library) containers are always the right default choice -- they are faster, more robust, and contain far fewer bugs than anything you could hope to write yourself. There are times that they are not appropriate, but by the time you are ready to forego them in favor of some other solution, you will be experienced enough to not need to seek advice on these forums
You might see a perf difference for std::vector over a C array because vector throws exceptions on out-of-bounds access, meaning it has to check every access.
Only for random access (e.g. [] brackets) though, and only if exceptions are enabled, right? I would assume, perhaps incorrectly, that linear access through iterators at least amortizes the cost of two checks, if not avoiding it entirely -- perhaps modulo any funny-business one might be doing with the array size whilst walking it.
Only for random access (e.g. [] brackets) though, and only if exceptions are enabled, right? I would assume, perhaps incorrectly, that linear access through iterators at least amortizes the cost of two checks, if not avoiding it entirely -- perhaps modulo any funny-business one might be doing with the array size whilst walking it.
You might see a perf difference for std::vector over a C array because vector throws exceptions on out-of-bounds access, meaning it has to check every access. I don't recall if std::array throws on out of bounds access, but I'd imagine it does.
std::vector::at throws an exception, but std::vector::operator[] does not. Some light Googling indicates that std::array is similar.
Most implementations of the C++SL will do range checking, e.g. as an assertion, in debug builds, I believe at least some versions of VC++ went for range checking in all builds, but with a separate flag to disable them.
std::vector::at throws an exception, but std::vector::operator[] does not. Some light Googling indicates that std::array is similar.
Yes, an implementation that does range checking and throws an exception for operator[] of the standard containers is non-conforming. Most implementations are conforming in this respect, with an optional "debug" mode that needs to be explicitly enabled but is explicitly non-conforming. At least one well-known vendor is non-conforming by default and the developer needs to go out of her way to make the implementation conform to the standard by flipping switches and pre-defining constants, which leads to lots of questions about poor performance in forums like these.