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)
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
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)
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
line 11 of that code snippet is wrong. It should be delete[], not delete.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
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
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343