1

I'm a Java programer lost in C++ and pointers :D

I have an array of pointers to Bucket-Objects

Bucket<E>* index = new Bucket<E>[2];

I initialize it like this:

index[0] points to Bucket1
index[1] points to Bucket2

And then I want to double the size of the array and link the additional entries like this:

index[0] points to Bucket1
index[1] points to Bucket2
index[2] points to Bucket1
index[3] points to Bucket2

I have this code so far, which generates copies of the Bucket-Objects and I don't want that!

for (size_t i = 0; i < newSize; ++i)
{
    if (i < oldIndexSize)
       newIndex[i] = index[i];
    else
       newIndex[i] = index[i - oldIndexSize];

} 
5
  • in fact, you want a table of pointers to Bucket<E> elements. Bucket<E>** index = new (Bucket<E> *)[2]; Commented Apr 2, 2012 at 16:38
  • You're starting out wrong. Unless you made a copy and paste error, your first line of code is not an array of pointers, it's just one pointer that you initialise to hold two Buckets. Did you mean Bucket<E>**? Commented Apr 2, 2012 at 16:39
  • Or, you'd probably be better of with a vector of pointers. Commented Apr 2, 2012 at 16:40
  • yes, I'm starting out wrong, I need Bucket<E>** index = new Bucket<E>*[2]; Commented Apr 2, 2012 at 16:56
  • @user1308532: If you really need to use raw pointers, and if you find the double pointer syntax (Bucket<E>**) confusing, you may want to use some intermediate typedef for better code clarity, e.g. typedef Bucket<E>* BucketPtr;, and then allocate with BucketPtr* index = new BucketPtr[2];. Commented Apr 2, 2012 at 22:06

4 Answers 4

2

I have an array of pointers to Bucket-Objects

No, you have a pointer to an array of Bucket objects. An array of pointers to Bucket objects would look like this:

Bucket<E> * index[2];

A dynamically allocated array of pointers to bucket objects would be declared like this:

Bucket<E> ** index = new Bucket<E>*[N];

But what you should probably be using is a vector of shared pointers to bucket objects, like this:

std::vector<std::shared_ptr<Bucket<E>>> index;
Sign up to request clarification or add additional context in comments.

4 Comments

Using std::shared_ptr mechanically, without knowing why, and without understanding its problems, is a recipe for disaster. On the other hand, he should definitely be using std::vector, and not be allocating an array of pointers. (In something like 25 years of C++, I've never yet found a use for new[].)
@JamesKanze: It wasn't mechanical. I do know why, because I read his question. He wants to share ownership between different elements of his array.
Where do you see that? I didn't see anything about ownership.
@JamesKanze: I see no other sensible way to interpret his question. A vector of Bucket objects wouldn't work. A vector of unique pointers wouldn't work. The only other possibility I can see is that he has a separate collection of objects and that he would want a vector of dumb pointers acting as references into that collection. That case seemed unlikely to me, so I ignored it.
1

I'm a Java programer lost in C++ and pointers :D

In modern C++, raw pointers are rarely used. You should prefer smart pointers (like shared_ptr or unique_ptr). This gives several benefits, like helping make your code exception-safe, simplifying your code (e.g. resources are automatically destructed and released, there is no need to call an explicit delete), etc.

I have an array of pointers to Bucket-Objects

Bucket* index = new Bucket[2];

If the Bucket instances you store in the array are not shared, you can use vector<unique_ptr<Bucket>>. Else, if there is a shared semantics, you may want to use vector<shared_ptr<Bucket>>. You can use vector::push_back() method to add new instances of Bucket's to the vector, which will dynamically grow. If you choose to use shared_ptr<> smart pointer, allocate the instances of Bucket's using make_shared instead of raw operator new.

There is a very interesting series of STL lessons on Channel 9, by the STL maintainer working in Visual C++ Team. You may want to consider part 1 (sequence containers) and part 3 (smart pointers).

Comments

1

Do not re-invent the wheel. Use std::vector<Bucket<E> > if you want a resizeable array. If you don't want the buckets to be copied over, you will have to use some indirection. For instance with smart pointers:

std::vector<std::shared_ptr<Bucket<E>>> index {std::make_shared<Bucket<E>>()/*, ...*/}

2 Comments

@user1308532: In that case, the question should be tagged homework. However, especially in University, good code should be chosen over hackish C-like stuff.
@bitmask: It's better (less overhead, better locality) to use make_shared instead of raw operator new with shared_ptr.
0

If it is an Array of pointers it will not creat the copy of object it will be only a copy of a pointer and it seems that this code does the thing you want.

Comments

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.