Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 'It's worth noting that the actual memory usage for the object is the same' No, it isn't! It differs by sizeof(std::vector<float>) - sizeof(std::vector<float>*) on either stack or heap ... Commented Feb 14, 2014 at 1:13
  • @πάνταῥεῖ: Can you elaborate on what you mean? Why would the size be different? Commented Feb 14, 2014 at 1:19
  • Adding another std::vector<float> object to the heap (the OP's 2nd case), would require additional sizeof(std::vector<float>) (eventually aligned) bytes from there, wouldn't it?? Commented Feb 14, 2014 at 1:23
  • I don't think so. The allocation should be exactly the same size no matter where it is. If it wasn't, then array traversal and pointer arithmetic would be different, depending on whether the data was on the stack or heap. Commented Feb 14, 2014 at 1:29
  • I think you misunderstood: For the data managed internally by the std::vector<> object, there's no difference (as I also mentioned in my answer). But for the std::vector<> object itself managed on heap or stack, there is (at least sizeof(std::vector<float> - sizeof(std::vector<float*>)! It's a bit nitpicking, but might matter when calling a function recursively and using a stack allocated vector. For the overall memory consumption of a process there's no considerable difference at all. Commented Feb 14, 2014 at 1:40