2

I have a question about converting from std::vector to an array. I have a vector of pointers to object. How can I convert it to an array of pointer to objects in C++?

The vector is std::vector<pin*> *_PINS

I want to convert it to pin** pins_arr

I've tried everything that has been suggested in here but it doesn't work

I guess the reason why it's not working is because I have pointer to object as a type instead of basic type.

Would you please help me with this? I've been stucked for the whole morning.

Thank you,

3
  • 3
    The way in which you are using this vector is completely wrong to begin with. Storing a pointer to a vector is almost always wrong, as is using a pointer type as the template parameter. Commented Mar 14, 2012 at 21:31
  • Yep, doubtless you should try to eliminate pointers from the program entirely. Please tell us what went wrong with the existing suggestions. Also, if _PINS->data() doesn't work, try & (*_PINS)[0], which is the C++03 version of the same thing. Commented Mar 15, 2012 at 8:39
  • Looks like nobody's mentioned yet that _PINS is an illegal identifier. It's better to avoid leading underscores, and (in my opinion) capitals in general. An underscore followed by a capital is reserved for the system, and the compiler might give you warning messages. Commented Mar 15, 2012 at 8:43

3 Answers 3

2

Edit: if you really insist on doing a conversion like this, I suppose you could do something on this order:

std::vector<PIN *> *_PINS;

_PINS = new std::vector<PIN *>;

// make life a little easier:
std::vector<PIN *> tmp = *_PINS;

PIN *pins = new PIN *[10];

for (int i=0; i<10; i++)
    pins[i] = tmp[i];

PIN **ppins = &pins;

I have to agree with Ed.S though -- what you're doing here defeats most of the purpose of using std::vector in the first place, and frankly strikes me as a bit silly.

Sign up to request clarification or add additional context in comments.

15 Comments

Hi,I've tried this and it doesn't work. It gives me this error *** glibc detected *** free(): invalid pointer: 0x0000000000000031 *** Do you know why this is happening? Thanks Jerry,
I think _PINS->data() would be more idiomatic in C++11.
You need to be aware that the returned pointer is only valid as long as the vector does not change. If you add or remove items, it might reallocate and invalidate your pointer.
@EdS: it also matters when vector is empty, that's why C++11 added data() member.
A vector is dynamic, so basically you get the user to enter whatever you need to make a PIN, and use that to create a PIN, which you push onto the vector.
|
2

the member function data() returns the array, so _PINS->data() should work...

Comments

1

So, first off; why are you using a vector of pointers at all? Why are you storing a pointer to a vector?! This circumvents the vector's ability to manage memory for you and is almost certainly wrong. Let the vector do what it was meant to do, it handles dynamic allocation and clanup for you behind the scenes.

vectors guarantee that their memory is stored in contiguous space, so the address of the first element can be used as a pointer, i.e., an array. When you store a pointer to a vector you once again eliminate it's ability to clean up deterministically based upon scope. The destructor will not run until you manually call delete on it.

You are essentially using a vector as an array here. Again, this is wrong. The vector itself is a lightweaght object, there is no good reason to dynamically allocate it. Pass a vector& to functions that need to use it.

However, and more importantly; you are doing it wrong. Just use this:

using std::vector;

//...

vector<pin> pins;  // that's it!  really!

3 Comments

Thanks Ed, I will try this as well.
Hi Ed, I can't really. I want to create an aggregate (say class A), which contain a list of pointers to objects of another class (class B). The reason I need to use pointers is because I don't want objects of class B to be destroyed when I destroy an object of class A. However I need to be able to loop through the list of B objects if I want to. I couldn't think of another way really. Would you please help?
@BinhVanPham: Wow, still working on a problem from more than two years ago? You have std::shared_ptr available to you. However, it sounds to me like you may have a design problem on your hands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.