Archived

This topic is now archived and is closed to further replies.

Question on dynamic Arrays in C++

This topic is 5276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I create a 2D array, which is part of a class, but the size is determined when the constructor is called. I tried several ways but can seem to get it right. I think I need something like:
CArray::CArray( usigned int x_size, unsigned int y_size )
{
    *int array[][] = new int[ x_size ][ y_size ];
}
[/CODE]

Whats the right way to delare a dynamic array in the class'' header and enable me to specify the dimensions when the constructor is called?

   

Share this post


Link to post
Share on other sites
Allocating:

int **array = NULL;
array = new int*[ x ];

for( int i; i<x; i++ )
{
array[ x ] = new int[ y ];
}



Deallocating:
 
for( int i; i<x; i++ )
{
delete[] array[ x ];
}

delete[] array;
array = NULL;


[FINAL EDIT] Geez, sometimes I hate these forums..


.lick


[edited by - Pipo DeClown on July 7, 2003 8:47:09 AM]

Share this post


Link to post
Share on other sites
Here''s one way, using standard C++ dynamic arrays :

#include <vector>

class MyClass
{
// 2D array as a dynamic array of dynamic arrays

std::vector< std::vector<int> > array;
public:
MyClass( unsigned int x, // Constructor

unsigned int y )
: array(x,std::vector<int>(y)) // Initialize with x arrays of y ints

{} // Nothing else to do


};


And another way, using Boost

#include <boost/multi_array.hpp>

class MyClass
{
// creates a 2D array of ints

boost::multi_array<int,2> array;
public:

MyClass( unsigned int x, // Constructor

unsigned int y )
: array(boost::extents[x][y]) // Initialize the array to be x by y

{} // Nothing else to do

};


Pros : Resizable. No cleanup needed.
Cons : Unfamiliar to many.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
The Boost library is a set of extensions to the C++ standard library. There is some very nice stuff to be found there once you know how to use the C++ standard library

quote:
From the Boost website
The Boost web site provides free peer-reviewed portable C++ source libraries. The emphasis is on libraries which work well with the C++ Standard Library. One goal is to establish "existing practice" and provide reference implementations so that the Boost libraries are suitable for eventual standardization. Some of the libraries have already been proposed for inclusion in the C++ Standards Committee''s upcoming C++ Standard Library Technical Report.






[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites