0

I need a two dimensional array filled with instances of different derived types my code looks like this:

std::array<std::array<std::shared_ptr<Base>, 1>, 1> b;
b[1][1] = std::shared_ptr<Base>(new Derived(x, y));

The code compiles but there is some form of memory leak in std::__shared_weak_count::__release_shared() during the execution of the second line.

My question is: How can I properly create a two dimensional array of derived classes?

4
  • 8
    b[1] is out of bounds Commented Apr 24, 2015 at 9:34
  • 2
    wow :( classic case of to long programming session Commented Apr 24, 2015 at 9:36
  • 2
    Just like T a[1][1]; a[1][1] = v; is going out of bounds, your code is going out of bounds as well. Commented Apr 24, 2015 at 9:36
  • 1
    [OT]: You may directly use std::make_shared<Derived>(x, y) instead of std::shared_ptr<Base>(new Derived(x, y)) Commented Apr 24, 2015 at 11:54

1 Answer 1

1

As mentioned in the comments b[1] is out of bounds in your example.

In general an std::array (as well as C-array, std::vector, and others) of length n allows you to use indices 0, .., n - 1 on it.

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

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.