CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    166

    how can I use a pointer to a std::vector?

    I have a pointer to a vector that contains instances of class Test:

    Code:
    vector<Test*>* testVec;
    If I do this:

    Code:
    Test* one = new Test();
    tectVec->push_back(one);
    this crashes. What do I need to do to declare the dynamic memory so this works?

    Thanks!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: how can I use a pointer to a std::vector?

    First a question: Why are you using dynamic allocation at all here? It's usually not necessary when working with vectors, and just tends to gum up the works when done wrong.

    Second, you'll need to make sure you've allocated the vector itself, since testVec is just a pointer.

    By the way, the correct terminology is that the vector contains pointers to Test. Containing instances is usually easier to deal with, but that isn't what your code does.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: how can I use a pointer to a std::vector?

    You need to allocate the vector as well.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Mar 2009
    Posts
    166

    Re: how can I use a pointer to a std::vector?

    I need to use dynamic memory because I am sending the vector to another thread.

    How does the allocation work? I tried this code below and it crashes on the reserve() call.

    Code:
    vector<Test*>* testVec;
    testVec->reserve(10*sizeof(Test*));
    Test* one = new Test();
    testVec->push_back(one);
    Thanks!
    Last edited by ekhule; March 16th, 2012 at 12:25 PM.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: how can I use a pointer to a std::vector?

    Quote Originally Posted by ekhule View Post
    I need to use dynamic memory because I am sending the vector to another thread.
    Okay; that isn't strictly true, but in some cases it can be easier to work with dynamic memory for cross-thread objects.

    That doesn't explain by both the vector *and* the Test objects within it need to be pointers, though.

    How does the allocation work? I tried this code below and it crashes on the reserve() call.

    Code:
    vector<Test*>* testVec;
    testVec->reserve(10*sizeof(Test*));
    Test* one = new Test();
    testVec->push_back(one);
    Thanks!
    You still are not allocating the vector itself. There is no vector in existence yet for the reserve() call to operate on, just a pointer!

  6. #6
    Join Date
    Jan 2009
    Posts
    596

    Re: how can I use a pointer to a std::vector?

    Quote Originally Posted by ekhule View Post
    I need to use dynamic memory because I am sending the vector to another thread.

    How does the allocation work? I tried this code below and it crashes on the reserve() call.

    Code:
    vector<Test*>* testVec;
    testVec->reserve(10*sizeof(Test*));
    Test* one = new Test();
    testVec->push_back(one);
    Thanks!
    You are not creating the vector. What you are doing is like
    Code:
    SomeClass* obj;
    obj->someMethod();
    You need to create the vector using new. Also, the reserve() method parameter is how many objects to reserve space for, not how many bytes, so it should be
    Code:
    testVec->reserve(10);
    giving us:
    Code:
    vector<Test*>* testVec = new vector<Test*>;
    testVec->reserve(10);
    Test* one = new Test();
    testVec->push_back(one);

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: how can I use a pointer to a std::vector?

    Frankly, this issue should be easy for you to understand and solve by stepping the code in a debugger.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    Mar 2009
    Posts
    166

    Re: how can I use a pointer to a std::vector?

    Thanks everyone! Except S M A :P

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: how can I use a pointer to a std::vector?

    Well I didn't do much other than point out what most programmers would do when they have an issue... If that's bad so be it...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured