pointer to a vector

Discussion in 'C++' started by A, Feb 12, 2014.

  1. A

    A Guest

    There are 2 vectors each one having a structure

    struct MyStruct
    int a;
    std::string b;

    std::vector<MyStruct> v1;
    std::vector<MyStruct> v2;

    Now I want a pointer to which one I will use... v1 or v2

    std::vector<MyStruct> *v = (condition)? &v1 : &v2;

    Finally I access it using:

    for (unsigned i = 0; i < v->size(); i++)
    v->operator[](i).a = i + 1;

    My lack of understanding here is:

    a) does the above *v needs to be deleted? Isn't it just a pointer variable?
    Or it works differently when it points to a vector? Just to be clear, I
    don't actually need to delete v1 or v2. I just need to cleanup *v if

    b) what's the heap or stack or difference or advantage of them in relation
    to the above?
    A, Feb 12, 2014
    1. Advertisements

  2. A

    A Guest

    Actually I just found that I can use *v like this:

    (*v).a = i + 1;

    Cleaner to me than

    v->operator[](i).a = i + 1;

    However, my questions are still the same.
    A, Feb 12, 2014
    1. Advertisements

  3. A

    Jorgen Grahn Guest

    You don't need a pointer below -- a reference would have worked just
    as well, and the syntax would have been cleaner.
    No, and yes. You haven't done 'new', so you're not responsible for
    doing 'delete'. BTW, both of those are rarely needed in modern code
    -- if you use them a lot you're probably doing something wrong.
    No cleanup required. But I don't really understand what you're trying
    to say ...
    I don't understand that question. Please rephrase.

    Jorgen Grahn, Feb 12, 2014
  4. A

    A Guest

    No, I don't use normally new or delete. I use boost smart pointers and
    Good thing to know this rule of a thumb - using new = delete, not using it,
    then it is just a pointer.
    What I though is that if it is pointer to a vector then it is somehow
    different but obviously it is just a same pointer like any other. So yes,
    then no cleanup is required, it is just a pointer variable.
    A guy here was mentioning heap so I though it was related. Seems it is not:
    A, Feb 12, 2014
  5. A

    A Guest

    That's a good rule to remember. So it is just a plain pointer. Thanks!
    Yes, haven't been using new/delete in years since I discovered smart
    pointers and vectors.
    A, Feb 12, 2014
  6. A

    Stefan Ram Guest

    IIRC, the default constructor constructs an /empty/ vector.
    This means that /neither/ vector so far has a structure.
    Stefan Ram, Feb 12, 2014
  7. A

    Jorgen Grahn Guest

    I don't know which of them, but several of them seem rather confused.
    You'll get better answers here, with the right questions.

    Jorgen Grahn, Feb 12, 2014
  8. A

    A Guest

    Of course, I know that - that was just a dumb example. Thanks for pointing
    this out though. Much appreciate the effort.
    A, Feb 13, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.