
February 8th, 2011, 11:51 AM
#1
Dynamic Matrix
Hello,
i've a question: how can I get the access to all the elements of a dynamic matrix?
I create a dynamic matrix in this way:
Cout<<"insert matrix dimensions";
cin>>n;
int ** matrix= new int * [n];
for (int i=0;i<n;i++)
matrix [n]= new int [n];
//but when I try to initialize the element in this way:
for (int i=0;i<n;i++)
for (int J=0;j<n;j++)
matrix [i]*[j]=0;
the program gives me an error, how could I insert and modify all the location of the dynamic matrix?
Thanks a lot for attention

February 8th, 2011, 11:55 AM
#2
Re: Dynamic Matrix
matrix[i][j] = 0;
Note, you'll need to clean up that memory. Consider defining your matrix as a std::vector<std::vector<int>> instead, or with a boost::multi_array<int, 2>.

February 8th, 2011, 12:19 PM
#3
Re: Dynamic Matrix
yes, but in my code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Inserisci le dimensioni della matrice NxN\n";
cin>>n;
int **matrix = new int * [n];
for (int i=0;i<n;i++)
matrix[n]=new int [n];
for (int i=0;i<n;i++)
for(int j=0;j<n;j++)
> matrix [i][j]=0;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<matrix [i][j];
cout<<endl;
}
delete []matrix[n];
return 0;
}
the program returns:"Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary applyloadrules all
(gdb) "

February 8th, 2011, 12:30 PM
#4
Re: Dynamic Matrix
1. You have:
Code:
for (int i=0;i<n;i++)
matrix[n]=new int [n]; // should be "i" not "n"
2. your delete code is not correct
Code:
for (int i=0; i<n; ++i)
{
delete [] matrix[i];
}
delete [] matrix;
3. There are more efficient ways to allocate the matrix. A search on codeguru
should yield a better method.

February 8th, 2011, 12:30 PM
#5
Re: Dynamic Matrix
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Inserisci le dimensioni della matrice NxN\n";
cin>>n;
int **matrix = new int * [n];
for (int i=0;i<n;i++)
matrix[n]=new int [n];
for (int i=0;i<n;i++)
for(int j=0;j<n;j++)
> matrix [i][j]=0;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<matrix [i][j];
cout<<endl;
}
delete []matrix[n];
return 0;
}
I have highlighted the lines which are wrong.

February 8th, 2011, 12:49 PM
#6
Re: Dynamic Matrix
Originally Posted by Falkotux
yes, but in my code: ...
Please take a look at this alternative to your code:
Code:
int main()
{
int n;
cout<<"Inserisci le dimensioni della matrice NxN\n";
cin>>n;
// Create row pointers
int **matrix = new int * [n];
// Create the data for the matrix in one call.
int *matrix_data = new int[n*n];
// Create n*n matrix
for (int i=0; i < n; i++, matrix_data+=n)
matrix[i] = matrix_data; // point row pointers to matrix data
// delete n*n matrix
delete []matrix[0]; // delete the beginning of the matrix data
delete [] matrix; // delete the row pointers.
}
Compare this with your code. Hopefully you see the major difference in how the matrix is created.
In your version, you are calling new[]/delete[] n+1 times, while in the code above, there are only 2 calls, regardless of the size of the matrix. If n were 1000, you would be calling the allocator 1001 times, while the version above only calls it 2 times.
Basically this method:
1) saves time (since the allocator is only being called twice for new[] and delete[]),
2) Memory fragmentation is minimized
3) The resulting matrix has the same layout as a regular 2 dimensional array, in other words, the data is contiguous.
Regards,
Paul McKenzie

February 9th, 2011, 05:38 AM
#7
Re: Dynamic Matrix
It works! Thank you, and thanks for the new way of creating dynamic matrix (more efficient). This is the best programming forum i've ever seen. Thank you again
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
