|
-
March 16th, 2012, 10:45 AM
#1
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!
-
March 16th, 2012, 10:59 AM
#2
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.
-
March 16th, 2012, 10:59 AM
#3
Re: how can I use a pointer to a std::vector?
You need to allocate the vector as well.
-
March 16th, 2012, 12:16 PM
#4
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.
-
March 16th, 2012, 12:38 PM
#5
Re: how can I use a pointer to a std::vector?
 Originally Posted by ekhule
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!
-
March 16th, 2012, 12:39 PM
#6
Re: how can I use a pointer to a std::vector?
 Originally Posted by ekhule
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);
-
March 16th, 2012, 12:58 PM
#7
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.
-
March 16th, 2012, 02:59 PM
#8
Re: how can I use a pointer to a std::vector?
Thanks everyone! Except S M A :P
-
March 16th, 2012, 06:13 PM
#9
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|