ehrenfest
- 2,001
- 1
Code:
std::vector<float> *my_vectors;
does this create a vector of pointers or a pointer to a vector?
The discussion centers around the declaration of a pointer to a vector in C++, specifically the statement std::vector. Participants explore whether this creates a vector of pointers or a pointer to a vector, as well as the implications of memory allocation related to this declaration.
std::vector *my_vectors; creates a pointer to a vector, indicated by the placement of the asterisk.float.Participants express differing views on the nature of memory allocation related to the pointer declaration, with no consensus reached on the implications of the declaration or the definitions of memory association versus allocation.
Some participants note that the understanding of memory allocation may depend on specific definitions and interpretations of C++ behavior, which remain unresolved in the discussion.
std::vector<float> *my_vectors;
No. The statement int index; creates a variable named index. While this variable is associated with a chunk of memory, this declaration statement does not allocate memory, at least not in the sense you are thinking. I certainly hope you will not write code that accesses the value of the variable index before you assign it a value.ehrenfest said:OK. So it actually allocates the memory for the pointer and also allocates the memory for the vector that is pointed to, right?
D H said:his declaration statement associates a variable with a chunk of memory. It does not, however, allocate memory, at least not in the sense you appear to be thinking.