About double pointers..

Hi all!
I'm typing in my dev since a month ago for taking much more confidence with the c++ (already met at high school).

I've formulated a basic script for create dynamics matrix:

1
2
3
4
5
6
7
int **G = new int*;
int n;
for(int m=0; m<4; m++) 
   G[m] = new int;	
for(int m=0; m<4; m++) 
   for(n=0; n<4; n++) 
      G[m][n] = m+n;


but I want to semplify it.
I think about the two "for" loop and I've tried to do this:

1
2
3
4
5
6
7
8
int **G = new int*;
int n;
for(int m=0; m<4; m++) 
{
   G[m] = new int;
   for(n=0; n<4; n++) 
      G[m][n] = m+n;
}


but I don't know why appears only the black console and next crash!
Seriously I've reflected about it, but IMHO have the same result :(

Thanks in advance!!
It looks like they should be the same to me too, but you have made a few mistakes. new int* allocates 1 pointer and new int allocates 1 integer, so G[0][0] is the only valid element you should access. The rest is out of bounds so what you have here is undefined behaviour which means you can't be sure what will happen.

You probably want to allocate arrays of size 4 instead. new int*[4] allocates 4 pointers and new int[4] allocates 4 integers. Note that you should now use delete[] instead of delete when freeing the memory.
Thanks Peter87 for the instant answer :)

I know that i can consider a pointer like an array and so I can create it dynamically for example so:

1
2
3
4
int *p=new int;
p[0]=0;
p[1]=23;
.... 


I create only a new address in which I can store an integer but at the same time with the array's notation I can do a dynamic array!

At the same way I can do it for a matrix, but in a real terms I must make a program that ask you the matrix's dimension (m rows and n columns) so i thinked something like:

1
2
3
4
5
6
7
8
9
...
int **G = new int*[m]; //creates an array of pointers 
for(int i=0; i<m; i++) 
{
	G[m] = new int; //creates a start point for an array of int
	for(int j=0; n<4; n++) //then i 'feed' every element ;)
		G[m][n] = m+n; 
}
...


This is what I really searched for :P Do you think is a good way? There is another better
Is this the best way? Probably not. It would have been easier use to std::vector if you knew how.

Do I recommend you to use std::vector in this program? Not necessarily. What you are doing is a good exercise in how pointers and arrays work and that is something that every C++ programmer should know. After you have become comfortable with pointers and arrays you will better be able to appreciate std::vector and to understand how it really works.

In your updated code you need to allocate an array on line 5. You also need to make sure you use the correct variables as indices and the loop condition for the inner loop is incorrect. Otherwise it looks like it's going to work.
Last edited on
thank you very much for spnt your time for me ;) I've correct the code instantly :) and i've understand a little bit more the c++ :P

The reason for my choise of not using std::vector is dictated by my professor ;) he want a program that using only the <iostream>!

IMHO I think it's a really cool stuff because he force me to squeeze any drop of my creativity and at the same time I can understand much more well the potentiality of this language and how it works in the inside, exactly just like you said.

Thanks very much again!! :)

P.S.: the errors about indices and loop condition are caused only by my haste to write :P
Last edited on
Topic archived. No new replies allowed.