You are here:

C++/dynamic allocation

Advertisement


Question
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: > I know how to declare dynamically an array of objects of class MyClass:
> MyClass **array=new MyClass *[1000];

Well, you are not allocating an array of objects of class MyClass. You are allocating an array of pointers to objects of class MyClass.

> how could I declare a 2-dimensional array dynamically
> MyClass **array=new MyClass *[1000][1000];
> What would the correct syntax be?

When you allocate an array with dynamic storage duration, new returns the address of (a pointer to) the first element of the array. There is no 2-dimensional array in C or C++, it is simulated by an array of which where every element is an array.

int a [20] [30] ; // an array of 20 elements. each element is an array of 30 ints.

new MyClass *[1000][1000] ; // an array of 1000 elements. each element is an array of 1000 pointers to MyClass

The syntax therefore is (simplified via a typedef):

   typedef MyClass* pointer_type ;
     
   pointer_type (*array)[1000] = new pointer_type [1000][1000] ;

   array[50][200] = 0 ;





---------- FOLLOW-UP ----------

QUESTION: Hi Vijayan,

Thanks a lot for your answer, it was most useful.
What you suggested works great when done like that:

//x is an integer computed during runtime
typedef MyClass* pointer_type;
pointer_type(*array)[100]=new pointer_type[x][100];

But it won't compile when done like this:

//x and y are integers computed during runtime
typedef MyClass* pointer_type;
pointer_type(*array)[y]=new pointer_type[x][y];

I need to declare this 2D array fully dynamically because I don't know the values for x and y until the program executes. I'm used to C where I use malloc to allocate 2D arrays. The problem is that malloc won't work with classes. Can I allocate 2D arrays fully dynamically with C++ during runtime; kind of like malloc does in C? And once you do, how do you delete the array (empty the memory)?

Thanks again so much!!

Andres


Answer
In standard C and C++, the number of elements in an array has to be a constant known at compile time. If the number of elements are variables known only at run time, use a std::vector<> instead. see: http://www.cprogramming.com/tutorial/stl/vector.html

The std::vector<> will manage the memory automagically - you do not need to do anything to release the memory.
For example:

#include <iostream>
#include <vector>

class MyClass ;

int main()
{
   typedef MyClass* pointer_type ;

   int x, y ;
   std::cin >> x >> y ;

   std::vector< std::vector< pointer_type > > array ;

   array[0][0] = 0 ;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.