You are here:

C++/dynamic allocation

Advertisement


Question
 Hi,

How are you?
I know how to declare dynamically an array of objects of class MyClass:

MyClass **array=new MyClass *[1000];

But how could I declare a 2-dimensional array dynamically like that. The following won't work:
MyClass **array=new MyClass *[1000][1000];
What would the correct syntax be?

Thanks!
Andres


Answer
Hello Andres, thank you for the question.

Since you want a 2D array of pointers, I think that would need to be declared as a triple pointer. However, I've never declared one before, so I'm not sure if it'll work.

Try this:

MyClass ***array = new MyClass* [1000][1000];

And see if that works.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.