   Heap or stack?
  Which is preferable when using class objects such as my Socket class with SOCKADDR_IN and SOCKET as the only member variables?And also consider this:class CFoo {struct foodata {CSocket* sp;CSomeOtherClass otherclass;HANDLE handle;};public:void SetData(CSocket* sp);};Now I'd like to save maybe 10-20 of foodata structures in a vector of the Foo class. Should I dynamically allocate foodata structures and use a vector of pointers or should I simply use a vector of foodata structures?   
  I would use the stack there.Dave   ;  What Dave said. But also consider that a vector is going to store the data structures on the heap anyway. If you know you will never need more than 20 of those structures and that you'll always need more than 10, you're probably better off using a statically sized array.Regardless, since you are storing a CSocket *, watch out for memory leaks.   ;  if you know the dimmension of the data (it is fixed) and if it fits in the stack then surely you can use the stack. Heap is only when you don't know how much data you store and you want to allocate only the memory you need, or when you have huge ammounts of data, that doesn't fit into the stack.   ;  A std::vector will always* store objects on the heap.When storing objects in a std::vector, you should usually choose to store by value rather than by pointer. Only store by vector if you're going to be reordering the vector a lot and the stored objects are expensive to copy OR if you require polymorphic behaviour.*when using std::allocator   ;Quote:Original post by NitageA std::vector will always* store objects on the heap.When storing objects in a std::vector, you should usually choose to store by value rather than by pointer. Only store by pointer if you're going to be reordering the vector a lot and the stored objects are expensive to copy OR if you require polymorphic behaviour.*when using std::allocate()Typos fixed ;)OR if you're deliberately making a vector of aliases to objects held somewhere else (trying to make a container of references is Bad News. Trust me.)However, in any of these cases, you should also consider making use of some sort of smart pointer, such as boost::shared_ptr.   