Ad
  • Default User Avatar

    Thank you for follow up. My understanding is that default stack size if 1MB or 8MB on most platforms (on embedded maybe way smaller) so this maybe determining factor, otherwise, let's keep it on stack!

  • Custom User Avatar

    A vector always uses more space than an array, and will always be slower if you know the length you need ahead of time. A vector has to store the values on the heap, and store its pointer, length, and capacity on the stack. An array just keeps its length and values on the stack. The only reason I can think of to use a vector over an array is if the length of the list needs to change at runtime. If the length doesn't change at runtime, but is determined once at runtime and then fixed, it's usually a good idea to use a Box<[T]>, since those are still heap allocated but don't keep track of their capacity because they can't change length. A Box<[T]> can also help in the rare occasion that you want to use an array that's too big for the stack space the OS gives you.

  • Default User Avatar

    Thank you! Great advice! I wonder up to which size on which architectures, array makes more sense then vector ...

  • Custom User Avatar

    There's no need to use a vector here when you know the list will only ever be length 256. An array works fine and performs better.