[CODE]stdvector<float> *my_vectors;[/CODE]does this create a

  • Thread starter Thread starter ehrenfest
  • Start date Start date
AI Thread Summary
The discussion clarifies the distinction between creating a pointer to a vector and creating a vector of pointers in C++. The statement `std::vector<float> *my_vectors;` creates a pointer to a vector, not a vector of pointers. Memory allocation for the vector occurs only when it is explicitly instantiated, while the pointer itself is merely associated with a memory address. The conversation emphasizes the importance of initializing pointers before use, as accessing them without assignment can lead to errors. The difference between associating a variable with memory and allocating memory is highlighted, with examples illustrating how to properly assign values to pointers. Additionally, the proper way to allocate memory for an array of floats using `std::vector<float> my_vectors[100];` is discussed, along with the implications of using methods like `push_back()` for adding elements to the vector.
ehrenfest
Messages
2,001
Reaction score
1
Code:
std::vector<float>  *my_vectors;

does this create a vector of pointers or a pointer to a vector?
 
Technology news on Phys.org
It creates a pointer to a vector. You can tell by seeing where the * is.
 
If you wanted a vector of pointers, move the * in front of float.
 
OK. So it actually allocates the memory for the pointer and also allocates the memory for the vector that is pointed to, right?
 
Well, you might want to try something like this.

Struct foo{
float i;
}
Class bar{
private:
vector <foo*> vec;
public:
//constructor, destructor, etc..
}

Remember to watch out for datatype differences when using functions from std:vector. You might need to do some clever programming the implementation of the class. It all depends on what your trying to do with this.
 
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?
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.

The statement int * index_pointer; similarly creates a variable, this time named index_pointer. Like the simpler index declaration, this 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. Just as with the simple non-pointer variable, it is a bad idea (in this case, a very, very bad idea) to access the index_pointer without first assigning a value to it. So how to assign a value to it? There are many ways: index_pointer = &index; makes index_pointer point to the memory associated with the existing variable index. index_pointer = new int(); allocates a new chunk of memory. This is what I think you mean by memory allocation.

The only difference between the statement in the OP and the above declaration is the type of pointer. Don't get drawn in by the complex type. Your my_vectors and my index_pointer are pointers.
 
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.

What is the difference between "associating" something with a chunk of memory and allocating the chunk of memory to that thing?

When my computer executes that line from my OP, it takes the string "my_vectors" and adds it to its hash map next to 600000 or wherever memory address it gives to the pointer. Then it goes to memory address 600000 and writes 600001 or whatever the address of the actual vector is. Then it increments the stack pointer 600002 and goes on to the next line.

Is that at all right?
 
std::vector<float> my_vectors; "associating" says I'm going to create an array of floats at some point and I'm going to refer to it as my_vectors, make a note of it - it doesn't allocate any memory.
So doing my_vectors.front() or my_vectors[0] would be an error.

std::vector<float> my_vectors[100] would also allocate the memory for 100 floats,
You can then do my_vectors[0]=1.23 upto my_vectors[99]=1.23
Doing 'my_vectors.push_back(1.23)' 100 times would do the same thing
 
Last edited:

Similar threads

Back
Top