0

I have an array of pointers:

Hotel *hotels[size];
  for (int i = 0; i < size; ++i)
    hotels[i] = new Hotel();

And I want to insert an object in this array after some object with name I know:

cin >> tmp_name;
for (int i = 0; i < size; i++) {
  if (hotels[i]->get_name() == tmp_name) {
    hotels[size] = new Hotel();
    size += 1;
    Hotel *tmp_hotel;
    tmp_hotel = hotels[i+1];
    hotels[i+1]->fillHotel();
    for (i = i + 2; i < size; i++) {
      hotels[i] = tmp_hotel;
      tmp_hotel = hotels[i+1];
    }
    break;
  }
}

What I do wrong?

UPD: My solution:

cin >> tmp_name;
for (int i = 0, j = 0; i < size; i++, j++) {
    new_hotels[j] = hotels[i];
    if (hotels[i]->get_name() == tmp_name) {
        new_hotels[j+1]->fillHotel();
        ++j;
        system("clear");
    }
}

hotels[size] = new Hotel();
++size;
for (int i = 0; i < size; i++) {
    hotels[i] = new_hotels[i];
}
3
  • What is the issue you have? Does the code crash (I assume so, as you increase size but you don't actually allocate more storage for your array), does it not give the correct result, does it not compile, ...? Commented Oct 1, 2016 at 8:46
  • 5
    This looks a lot like an almost literal translation from Java to C++. That's bad. Use std::vector instead of getting into trouble with naked pointers and new. I also have the feeling that you are using a non-standard GCC extension which allows you to specify an array's length at runtime, and that you never delete your objects. Commented Oct 1, 2016 at 8:57
  • Also, hotels[i + 1] will be out of range when i reaches its maximum value in the loops. Commented Oct 1, 2016 at 9:31

1 Answer 1

3

I can see different errors in your code.


For example:

Hotel *hotels[size];

size should be a constant expression and something let me think this is not the case. VLA are not part of the C++ standard. In short you cannot allocate dynamic memory on the stack. The proper initialization should be:

Hotel* hotels = new Hotel*[size];

The line in the loop:

hotels[size] = new Hotel();

you're actually accessing out of bounds of your array: size index is some memory is not included in your array and this will produce an undefined behaviour.


Another strange line is the following:

size += 1;

Despite the fact that confirms size is not a constant, you cannot increase your size of vector simply changing that variable. You're actually just changing a variable size, but the allocated memory for your array will be the same.


How resolve?

In order in increase (or change) the size of an array, the solution is almost always to create a new array, copy the old one. In your case that solution is pretty reasonable because you should copy just pointers and not entire objects.

There are a lots of question on S.O. where this topic is, for example here.

Despite of that, I strongly suggest you to use the most practical alternative, that is to use a real C++ code.

The most efficient class is std::vector which is a C++ way to handle dynamic array.

Finally, you should also consider the std::unique_ptr<T> class to handle dynamic memory and pointers.

The final solution will be a class:

std::vector<std::unique_ptr<Hotel>> hotels;
Sign up to request clarification or add additional context in comments.

2 Comments

I'd say the final solution should be std::vector<Hotel>.
@ChristianHackl I cannot know which obscure reasons are behind the fact he want to use pointers. Since the code level, I bet the simplest solution will be the "final" solution. Anyway I propose std::unique_ptr only to propose something more comparable with his code!

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.