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

  • Thread starter ehrenfest
  • Start date
In summary, when you declare a variable with the asterisk, it creates a pointer to a vector, but does not allocate memory. You can assign values to it, but be careful not to access the variable before you assign it a value.
  • #1
ehrenfest
2,020
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
  • #2
It creates a pointer to a vector. You can tell by seeing where the * is.
 
  • #3
If you wanted a vector of pointers, move the * in front of float.
 
  • #4
OK. So it actually allocates the memory for the pointer and also allocates the memory for the vector that is pointed to, right?
 
  • #5
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
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.
 
  • #7
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?
 
  • #8
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:

1. What is the purpose of [CODE]stdvector *my_vectors;[/CODE]?

The purpose of this code is to create a pointer to a vector that contains floating-point numbers. The vector can then be used to store and manipulate a collection of float values.

2. How is a vector of floats different from a regular array?

A vector of floats is a dynamic data structure, meaning its size can change at runtime, whereas a regular array has a fixed size. Vectors also have built-in functions for adding, removing, and accessing elements, making them more versatile than arrays.

3. Why use a pointer to a vector instead of just a regular vector?

Pointers allow for more efficient use of memory and can be passed as function parameters, whereas a regular vector is copied each time it is passed. Pointers also allow for easier swapping or reassigning of vectors.

4. How do you add elements to a vector of floats?

To add elements to a vector, you can use the push_back() function, which adds the element to the end of the vector. Alternatively, you can use the insert() function to add an element at a specific position in the vector.

5. Can a vector of floats store integers or other data types?

No, a vector of floats can only store floating-point numbers. If you need to store integers or other data types, you would need to create a separate vector for each data type or use a different data structure such as a map or list.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
7
Views
856
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
6
Views
840
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
463
Back
Top