Quick question regarding placement new, i'm trying to make a very simple vector however i'm struggling to understand how I assign an item to a particular index in the array, please could you point me in the right direction?
end is a pointer. Here is how to set the value in something being pointed at: *end = item;
Remember to watch out for when you have inserted so many items that your array is full. In those cases, you'll need to reallocate your array to make it bigger.
I see this: *allocator[elems+1] = item
but this is a mess of misunderstanding. allocator is an array, therefore allocator[elems+1] is a (reference to) one element in that array, therefore *allocator[elems+1] is an attempt to pretend that allocator[elems+1] is a pointer and to dereference it, and the compiler doesn't like it when you pretend something that isn't a pointer is a pointer.
*end = item; would work. allocator[elems+1] = item; would work.
yeah I think i've misunderstood the concept. I've actually come across 2 other problems in the below code. #1 I get a heap corruption when reallocate() is invoked, and #2 if I run the code in main below, all 5 numbers print, but on line 2 it prints a weird int like -857842 and can't see what's wrong either in print() or push_back()
Remember that arrays are zero-indexed. So, if you have 1 element in the array, that element is stored in allocator[0]. and the next new element you put into the array will go into allocator[1] .
(BTW, allocator seems like a really weird name to call the array you're using to store the vector elements. An allocator is an object that performs allocation, not storage space.)