954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ arrays

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

thilinam
Light Poster
35 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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;
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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;
}
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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 isyes, delete each array first before deleting p itself.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

wildgooe and WaltP,
Thanks for correction.

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

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

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...

u8sand
Junior Poster
131 posts since Dec 2008
Reputation Points: 78
Solved Threads: 15
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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;
u8sand
Junior Poster
131 posts since Dec 2008
Reputation Points: 78
Solved Threads: 15
 

I would recommend a reading to http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16 for a complete reference.
None of the above has highlighted that the above techniques will not make the matrix contiguous except post#3.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You