2

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

2
  • 3
    suggest your benchmark your precise situation. Commented Sep 19, 2009 at 2:36
  • @Wheat, Couldn't agree more with you. Commented Sep 19, 2009 at 3:00

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is WRONG!
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.