Please tell me how to create a multi diminsional array using pointer(dynamic array, set size at run time).

Recommended Answers

All 13 Replies

Declare pointer to pointer variable,

int **ptr;

How many one dim arrays?

p=new int*[4];

What is the size of each one array?

*(p+0)=new int[3];
 *(p+1)=new int[3];
 *(p+2)=new int[3];
 *(p+3)=new int[3];

How to freed allocated memory?

delete []p;

I'd recommend a container class where you pass two axis but gets resolved to a single array allocation.

This is only one example of how to solve this!

class IntArray
{
private:
	int *pInt;
	unsigned int nWidth;
	unsigned int nHeight;

public:
	IntArray( unsigned int w, unsigned int h );
	~IntArray();
	void Set( unsigned int x, unsigned int y, int val );
	int Get( unsigned int x, unsigned int y );
};

void IntArray::IntArray( unsigned int w, unsigned int h )
{
	pInt = new int[ w * h ];
	nWidth = w;
	nHeight = h;
}

IntArray::~IntArray()
{
	if (NULL != pInt)
	{
		delete[] pInt;
	}
}

int IntArray::Get( unsigned int x, unsigned int y )
{
	return pInt[ y * nWidth + x  );
}

void IntArray::Set( unsigned int x, unsigned int y, int val )
{
	pInt[ y * nWidth + x ] = val;
}

How to freed allocated memory?

delete []p;

Don't you also have to delete each p array? :icon_wink:

If you aren't sure, the answer is yes, delete each array first before deleting p itself.

Each new[] array requires a delete.
So in that other variation, each branch leg has to be deleted first then the backbone.

wildgooe and WaltP,
Thanks for correction.

delete has two forms:
1. delete pointer - deallocates a single element
2. delete []pointer - deallocates arrays of elements.

You could use std::vector's of std::vector's - then you don't have to worry about new() and delete().

I would recommend:

int **array;
int sizeX,sizeY;
sizeX = 5; // set to whatever
sizeY = 5; // set to whatever
array = new int*[sizeY];
for(int i = 0; i < sizeY; i++)
    array[i] = new int[sizeX];

// Then to delete:
for(int i = 0; i < sizeY; i++)
    delete array[i];
delete [] array; // i think you need the [] here but not sure.

---------------------
This is what i used in something i made, but remade it here in 5 mins. No guarantee it works -.- Even though it should...

line 11 of that code snippet is wrong. It should be delete[], not delete.

O Sorry, it don't let me edit now -.-
Here:

int **array;
int sizeX,sizeY;
sizeX = 5; // set to whatever
sizeY = 5; // set to whatever
array = new int*[sizeY];
for(int i = 0; i < sizeY; i++)
    array[i] = new int[sizeX];
 
// Then to delete:
for(int i = 0; i < sizeY; i++)
    delete [] array[i];
delete [] array;

post #3 presents a 1d array as if it were a 2d array. Thats a good way to do it, unless of course the requirement is to create a 2d array.

I did say that post#3 was an exception.
None of the above has highlighted that the above techniques will not make the matrix contiguous except post#3.

Why is contiguous memory even an issue here? Once the array is allocated it doesn't really matter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.