views:

376

answers:

2

Is there a performance penalty for working with a vector from the standard library in C++ instead of arrays in C?

+6  A: 

No, there's not (provided you compile with optimization so inlining can happen), provided you mean dynamically sized C "arrays" obtained with malloc.

Fixed-sized arrays in C will have the slight advantage that their address is fixed after linking (if global), or that they live directly on the stack rather than indirectly through a pointer to somewhere on the heap. I do believe there is still no performance difference; constant base addresses aren't faster than variable ones; both get loaded into a CPU register.

wrang-wrang
+1  A: 

The only real difference is that accesses with std::vector go through trivial functions. So long as you're using an appropriate optimization level such that those function calls get inlined, they'll be the same.

me22