Using double pointer for create random matrix

Hello,
could you please help me explain few steps from part of my program?
At first I am new here and beginner in c++ programing (I used to create programs in pascal before).
It isn't only my work so I am confused little bit.
I need to create random sized matrix and print it on screen.
This is the part of program and it is working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	srand ((unsigned)time(NULL)); // function for randomizing
	a=rand()%7+2; // creating random value of a - represents number of columns
	b=(a-1)+rand()%10; // creating random value of b - represents number of rows - can be bigger number than a
	int** matrix = new int* [b]; //this line I need explanation
	for (x=0; x<b; x++)
	{
		matrix[x] = new int[a]; //this line I need explanation too
		for (y=0; y<a; y++) 
		{
			matrix[x][y]=rand()%100;
			cout.width(a);
			cout  << matrix[x][y];
		}// creating random value of each matrix's unit and printing it on the screen well-arranged
		cout << endl;


I know first half of line means I am creating matrix which is pointer to pointer
int** matrix
but what does another part of line means?
= new int* [b];

And why in this line is missing the *?
matrix[x] = new int[a];

I think I am confused from NEW and * commands.
I have read some forums and documents about pointer and pointer to pointer however I didn't get it. Examples didn't suit for my problem or were too difficult to understand for my level of programing.
I am ok if you leave me a link to some good page.

For help and patience thanks in advance!
Last edited on
hi,
int** matrix = new int* [b];
int** matrix is pointer which point ot another pointer(s)
= new int*[b]; alocate an array of pointers to int (where b will indicate how many pointers to int will be in that array.
**matrix will point to the first *pointer in that matrix...
furter when you indexiing matrx you rach desired *pointer:
matrix[3]; is 3th pointer in that array where this third pointer points to an int.

to enter some value in that int, so you must reach that value using "last pointing pointer" for example:
matrix[3][4] = 4;
is something like
matrix->fourthPointer = 4;

thus if you have:
int****** matrix = new int*****[4] means that you'll have to initialize all the pointer with pointers and so on until you finaly initialize "last *" (one asterix pointer) which will point to some int, double or whatever..

1
2
And why in this line is missing the *?
matrix[x] = new int[a];


matrix[x] is final pointer which points an array of ints..
so it does not ned * (which is for pointer only not final variable.
here new int is (unnamed)varialbe wchih you access with pointer to enter some value.

EDIT:
to be more simple to understand:
AN POINTER WHICH POINTS TO ANOTHER POINTER MUST HAVE ONE MORE * ASTERIKS
(this is correct only in pointer to new pointer)

cherrs!
Last edited on
int** matrix is <==>(equivalent to) int matrix[][];
now
int* matrix[] <==> int matrix[][];

not

matrix[x] = new int[a];
is allocating memory for

1
2
3
matrix[0] = new int[a]; 
matrix[1] = new int[a]; 
matrix[2] = new int[a]; 


its like allocating memory to
*matrix[0] ( matrix array ) ;
Last edited on
ok thank you very much I am better know
however I have one more question
when I want to delete this allocated memory for matrix I can do that by
delete[] matrix;
or
delete[][] matrix;
Last edited on
1
2
3
for(int i = 0; i < b; ++i)  //where b is siizeof second dimenzion
     delete [] matrix[i]; //delete each pointer of that secon dimenzion
delete [] matrix;  //finaly delete matrix pointer 


1
2
3
[code]delete[][] matrix; // this operator does not exist(so it's error)
delete[] matrix;  //this would delete matrix pointer only but not other pointers
//at wich is matrix pointing 

Last edited on
so this is not enough?
1
2
3
4
5
6
7
8
9
10
11
12
	for (x=0; x<b; x++)
	{
		matrix[x] = new int[a];
		for (y=0; y<a; y++) 
		{
			matrix[x][y]=rand()%100;
			cout.width(a);
			cout  << matrix[x][y];
		}
		cout << endl;
	}
delete[] matrix;


EDIT:
if I would move this line
matica[x] = new int[a];
like this:
1
2
3
4
5
6
7
8
9
10
	int** matrix = new int* [b]; 
	matrix[x] = new int[a];
	for (x=0; x<b; x++)
	{
		for (y=0; y<a; y++) 
		{
			matrix[x][y]=rand()%100;
			cout.width(a);
			cout  << matrix[x][y];
		}

will something happen? or it will be the same (however it will make more sense to me)?
please answer both questions
Last edited on
this is OK cos only one pointer exist (*matrix)
1
2
3
4
5
6
7
8
9
10
11
12
13
	
for (x=0; x<b; x++)
	{
		matrix[x] = new int[a];
		for (y=0; y<a; y++) 
		{
			matrix[x][y]=rand()%100;
			cout.width(a);
			cout  << matrix[x][y];
		}
		cout << endl;
	}
delete[] matrix;


this is not OK
1
2
3
4
5
6
7
8
9
10
11
12
13
   int** matrix[b];
	for (x=0; x<b; x++)
	{
		matrix[x] = new *int[a]; //alocating pointer
		for (y=0; y<a; y++) 
		{
			matrix[x][y]=new int;
			cout.width(a);
			cout  << matrix[x][y];
		}
		cout << endl;
	}
delete[] matrix;


this is OK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	for (x=0; x<b; x++)
	{
		matrix[x] = new *int[a]; //alocating pointer
		for (y=0; y<a; y++) 
		{
			matrix[x][y]=new int;
			cout.width(a);
			cout  << matrix[x][y];
		}
		cout << endl;
	}
for(int i = 0; i < b; ++i)
     delete [] matrix[i];
delete [] matrix; 


each pointer must be deleted or you got memory leak.
each new must have one delete
each new[] must have one delete[]

EDIT:
have a look at this and you'll understand this aproach better:
http://www.cplusplus.com/forum/general/53785/
Last edited on
about deleting I got it
last problem - one more time:

if I would move this line
matica[x] = new int[a];
like this:
1
2
3
4
5
6
7
8
9
10
	int** matrix = new int* [b]; 
	matrix[x] = new int[a];
	for (x=0; x<b; x++)
	{
		for (y=0; y<a; y++) 
		{
			matrix[x][y]=rand()%100;
			cout.width(a);
			cout  << matrix[x][y];
		}
Last edited on
and why you put * into this line?
matrix[x] = new int[a];

sorry my mistake, it should be:
matrix[x] = new int*[a];

have a look at link which I posted in previous post,
copy the code and try to figure out how does this mehanizm work.(run it in you IDE)
if you figure it out and make your own copy by not looking into my code then you got the point.

aslo see this trick:
int& a = *new int;
reference to unnamed int variable
delete it so:
delete & a;

now you may be litle confused but that is also possible :D
this may be even more intersting for you:

1
2
int& a = *new int[3];
delete[]& a;


EDIT:
how to initialize such array??? :D

for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
	int& a = *new int[3];
	int* x = &a;
	int* y = &a + 1;
	int* z = &a + 2;
	*x = 1;
	*y = 2;
	*z = 3;
	cout << *x << endl << *y << endl << *z << endl;
	delete[]& a;
	cin.ignore();
	return 0;
}


that code has no sence but ilustrates just one more way how C++ is cool :D

there are also tricks on how to change reference to point to another location but that would be to much.
Last edited on
ok I was reading little however i am again confused about deleting
i have this in my code:
int** matrix = new int* [b];
so do I need this?
1
2
3
(int i = 0; i < b; ++i)
     delete [] matrix[i];
delete [] matrix; 

or just this?
delete[] matrix;

just to be sure
Last edited on
int** matrix = new int* [b];
so do I need this?
1
2
3
(int i = 0; i < b; ++i)
     delete [] matrix[i];
delete [] matrix; 


it depends what are you alocating with second dimension pointer.
consider those two very different examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	//FIGURE ONE
	int** matrix = new int*[3];
	for(int i = 0; i < 3; ++i)
		matrix[i] = new int;//single variable

	for(int i = 0; i < 3; ++i)
		delete matrix[i];//using delete for single varialbe
	delete[] matrix;

	//FIGURE TWO
	int** matrix= new int*[3];
	for(int i = 0; i < 3; ++i)
		matrix[i] = new int[2];//array of varialbles

	for(int i = 0; i < 3; ++i)
		delete[] matrix[i]; //using delete[] for array of variables
	delete[] matrix;

as you can see..
delete one varaiable is not same as delete[] array of variables or pointer
so my case is the second one - i think
because I have
int** matrix = new int* [b]
and
matrix[x] = new int[a];

however another question I have
Should I delete the ** firstly?
like this?
1
2
3
(int i = 0; i < b; ++i)
     delete [] matrix[i];
delete [] matrix; 

or should I delete * firstly?
1
2
3
(int i = 0; i < a; ++i)
     delete [] matrix[i];
delete [] matrix; 
yes, delete * firstly (that's "last pointer")
then delete ** (which is matrix and must be deleted last)

so answer is your second example:
1
2
3
int i = 0; i < a; ++i)
     delete [] matrix[i];
delete [] matrix; 


to understand why is that so, always deleter "intern" pointers first and "outer" pointer last.
thank you very much
Topic archived. No new replies allowed.