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

  • Thread starter ehrenfest
  • Start date
  • #1
2,012
1

Main Question or Discussion Point

Code:
std::vector<float>  *my_vectors;
does this create a vector of pointers or a pointer to a vector?
 

Answers and Replies

  • #2
85
0
It creates a pointer to a vector. You can tell by seeing where the * is.
 
  • #3
Vid
401
0
If you wanted a vector of pointers, move the * in front of float.
 
  • #4
2,012
1
OK. So it actually allocates the memory for the pointer and also allocates the memory for the vector that is pointed to, right?
 
  • #5
Vid
401
0
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.
 
  • #6
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
682
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.
 
  • #7
2,012
1
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?
 
  • #8
mgb_phys
Science Advisor
Homework Helper
7,774
12
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:

Related Threads for: [CODE]stdvector<float> *my_vectors;[/CODE]does this create a

  • Last Post
Replies
16
Views
7K
Replies
6
Views
2K
  • Last Post
Replies
8
Views
1K
  • Last Post
Replies
6
Views
4K
  • Last Post
Replies
3
Views
1K
Replies
5
Views
773
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
3
Views
3K
Top