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

  • Thread starter Thread starter ehrenfest
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the declaration of a pointer to a vector in C++, specifically the statement std::vector *my_vectors;. 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.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants assert that std::vector *my_vectors; creates a pointer to a vector, indicated by the placement of the asterisk.
  • Others suggest that to create a vector of pointers, the asterisk should be placed in front of float.
  • One participant questions whether the declaration allocates memory for both the pointer and the vector it points to, leading to a clarification that the declaration itself does not allocate memory in the expected sense.
  • A later reply discusses the difference between associating a variable with memory and actually allocating memory, prompting further inquiry into the mechanics of memory allocation during execution.
  • Another participant provides an example involving a struct and class to illustrate potential complexities when using vectors of pointers, emphasizing the need for careful programming based on data types.
  • There is a mention of how accessing uninitialized pointers or variables can lead to errors, highlighting the importance of proper memory management.

Areas of Agreement / Disagreement

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.

Contextual Notes

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.

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

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 7 ·
Replies
7
Views
1K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
7
Views
4K
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
4K