I started to write a function which had a pointer to a vector as a parameter so that it could modify that vector to output results (as the actual return value was an error code), when I started to think about the memory behind that.
Edit: Adding a new example here to make this clearer - I think I mostly have the idea from the answers already posted, but my example was poor in that it was not a good example of why to use a pointer - and did not exactly exhibit the properties I wanted.
(New example)
ErrorEnumType MyClass::MyFunction((other input parameters), std::vector<StructType> *output_vect) { // (do some processing) output_vect->clear(); bool process_complete = false; while(false == error_condition) { StructType new_element; // (do some more processing) output_vect->push_back(process_complete); // (do some more processing) } return No_Error; } I am aware that I could use a reference instead of a pointer to output_vect, but the linter I was using got mad at me for this, so I'm evaluating whether a pointer makes more sense.
(Old example) For example, if I have the following code:
std::vector<int> *vect = new std::vector<int>(); for (uint32_t i = 0; i < 10; i++) { std::cout << "Ptr: " << vect << " Size " << vect->size() << " Max Size " << vect->capacity(); vect->push_back(i); std::cout << " elements 0: " << (*vect)[0] << ", " << i << " :" << (*vect)[i] << std::endl; } And I run it, I get the following output:
Ptr: 0x557c393f9e70 Size 0 Max Size 0 elements 0: 0, 0 :0 Ptr: 0x557c393f9e70 Size 1 Max Size 1 elements 0: 0, 1 :1 Ptr: 0x557c393f9e70 Size 2 Max Size 2 elements 0: 0, 2 :2 Ptr: 0x557c393f9e70 Size 3 Max Size 4 elements 0: 0, 3 :3 Ptr: 0x557c393f9e70 Size 4 Max Size 4 elements 0: 0, 4 :4 Ptr: 0x557c393f9e70 Size 5 Max Size 8 elements 0: 0, 5 :5 Ptr: 0x557c393f9e70 Size 6 Max Size 8 elements 0: 0, 6 :6 Ptr: 0x557c393f9e70 Size 7 Max Size 8 elements 0: 0, 7 :7 Ptr: 0x557c393f9e70 Size 8 Max Size 8 elements 0: 0, 8 :8 Ptr: 0x557c393f9e70 Size 9 Max Size 16 elements 0: 0, 9 :9 It seems as though this could cause major memory issues - because if the vector needs to expand, it could be writing into space which is already being utilized, because it looks like that pointer does not change. Even running this over a much larger loop, this pointer looks like it is constant.
I'm still a (relatively) new programmer, and am not sure that I have the grasp on memory allocation that I would like to. Is my understanding correct - will this cause buffer errors and overwrite adjacent memory? Or is there some protection in std::vector that I am not considering?
(Edit): From some of the answers below, it appears that a layer of indirection between the pointer to the vector and where the elements of the vector are stored were what I was not accounting for. However, since the answers seemed to focus on how I allocated the vector, when I don't plan to use the new operator at all (just call the function like my_obj.MyFunction((other input parameters), &output_vect)), I wanted to make this more clear.
newfor a single object andnewfor arrays .vectordoes internally - allocating a singlevectorobject is what your code does, and it has absolutely nothing to do with the memory allocation which happens inside the vector object.