0

This is more of a theoretical question, and a non-serious one at that, but one I couldn't find an answer to online that I'm moreso just curious about.

If I were to create some class in C++ (we'll just call it Object) and made a dynamic array of this object type:

Object* objectArray = new Object[someSize];

Would there ever be some instance where I would want to make the elements inside the array dynamic as well?

I would imagine not, since this array already exists on the heap and there would be no reason to therefore specify that the elements inside should also be on the heap, if I understand how dynamic arrays work correctly.

Object** objectArray = new Object*[someSize];

which would let you do things like:

objectArray[0] = new Object(*parameters*);

I know this question is a bit nonsense, but I was intrigued if there were any use-cases for this type of thing. I couldn't find any similar posts on my own.

Still, if someone has asked this, feel free to just redirect me to that page, as I had no luck finding it myself.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    *and made a dynamic array of this object type.* -- `std::vector objectArray(someSize);`. Using `new[]` to create dynamic arrays is discouraged in this day and age of C++. – PaulMcKenzie Nov 30 '22 at 22:12
  • Related to that: [https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – drescherjm Nov 30 '22 at 22:18
  • C-string table perhaps? (With `Object = char`?) Then each c-string has a different length. So `objectArray[i]` would be a `char*` and the size is auto-discovered using `\0`. If that's what you're thinking about, I can turn it to an answer. – lorro Nov 30 '22 at 22:42

1 Answers1

3

One use case would be if you derive other classes from Object, and want the array to hold instances of different types of objects. In that case, you would need to have the array store Object* pointers, and then you would create each object individually. For example:

struct Object {
    virtual ~Object() = default;
};

struct Tree : Object {
    ...
};
struct Bush : Object {
    ...
};
struct Rock : Object {
    ...
};

... 

Object** objectArray = new Object*[someSize];

objectArray[0] = new Tree(...);
objectArray[1] = new Bush(...);
objectArray[2] = new Rock(...);
...

Of course, you really should use std::vector and std::unique_ptr to manage the dynamic memory, but that doesn't change what is described above, eg:

struct Object {
    virtual ~Object() = default;
};

struct Tree : Object {
    ...
};
struct Bush : Object {
    ...
};
struct Rock : Object {
    ...
};

... 

std::vector<std::unique_ptr<Object>> objectArray(someSize);

objectArray[0] = std::make_unique<Tree>(...);
objectArray[1] = std::make_unique<Bush>(...);
objectArray[2] = std::make_unique<Rock>(...);
...

Or:

std::vector<std::unique_ptr<Object>> objectArray;

objectArray.push_back(std::make_unique<Tree>(...));
objectArray.push_back(std::make_unique<Bush>(...));
objectArray.push_back(std::make_unique<Rock>(...));
...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Ah, I never considered inheritance cases for this type of thing. That's a really interesting answer. Yes, of course you'd just use a vector and unique_ptr in reality, but as you inferred, my question was less about correct syntax and much more about use cases within dynamic memory. Thank you, that was very introspective. – Micah Gant Dec 02 '22 at 05:21