Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jun 12, 2008 #1
    Code (Text):
    std::vector<float>  *my_vectors;
    does this create a vector of pointers or a pointer to a vector?
     
  2. jcsd
  3. Jun 12, 2008 #2
    It creates a pointer to a vector. You can tell by seeing where the * is.
     
  4. Jun 12, 2008 #3

    Vid

    User Avatar

    If you wanted a vector of pointers, move the * in front of float.
     
  5. Jun 12, 2008 #4
    OK. So it actually allocates the memory for the pointer and also allocates the memory for the vector that is pointed to, right?
     
  6. Jun 12, 2008 #5

    Vid

    User Avatar

    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.
     
  7. Jun 12, 2008 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  8. Jun 12, 2008 #7
    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?
     
  9. Jun 12, 2008 #8

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    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: Jun 12, 2008
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: [CODE]stdvector<float> *my_vectors;[/CODE]does this create a
Loading...