Following this nice example I found, I was trying to create a function that dynamically generates a 2D grid (two dimensional array) of int values.
It works fairly well the first couple of times you change the values but if crashes after that. I guess the part where memory is freed doesn't work as it should.
void testApp::generate2DGrid() {
int i, j = 0;
// Delete previous 2D array
// (happens when previous value for cols and rows is 0)
if((numRowsPrev != 0) && (numColumnsPrev != 0)) {
for (i = 0; i < numRowsPrev; i++) {
delete [ ] Arr2D[i];
}
}
// Create a 2D array
Arr2D = new int * [numColumns];
for (i = 0; i < numColumns; i++) {
Arr2D[i] = new int[numRows];
}
// Assign a random values
for (i=0; i<numRows; i++) {
for (j = 0; j < numColumns; j++) {
Arr2D[i][j] = ofRandom(0, 10);
}
}
// Update previous value with new one
numRowsPrev = numRows;
numColumnsPrev = numColumns;
}
I see 1 major bug:
// Assign a random values
for (i=0; i<numRows; i++){
for (j=0; j<numColumns; j++){
Arr2D[i][j] = ofRandom(0, 10);
}
}
Here the variable 'i' is used as the first index into 'Arr2D' and goes to a max of (numRows -1)
While in this code:
for (i=0; i<numColumns; i++)
{
Arr2D[i] = new int[numRows];
}
The variable 'i' is used as the first index but goes to a max of (numColumns-1). If numRows is much larger than numColumns then we are going to have a problem.
As a side note. When you try and clean up you are leaking the columns:
if((numRowsPrev != 0) && (numColumnsPrev != 0))
{
for (i=0; i<numRowsPrev; i++){
delete [ ] Arr2D[i];
}
// Need to add this line:
delete [] Arr2D;
}
Next thing to note.
This is truly not a good idea. Use some of the provided STL classes (or potentially boost Matrix). This looks like you are binding global variables and all sorts of other nasty stuff.
2-dim array in C++ with no memory issues:
#include <vector>
typedef std::vector<int> Array;
typedef std::vector<Array> TwoDArray;
Usage:
TwoDArray Arr2D;
// Add rows
for (int i = 0; i < numRows; ++i) {
Arr2D.push_back(Array());
}
// Fill in test data
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
Arr2D[i].push_back(ofRandom(0, 10));
}
}
// Make sure the data is there
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
std::cout << Arr2D[i][j] << ' ';
}
std::cout << '\n';
}
Related
I am trying to copy a matrix from an old matrix class to an armadillo matrix. Unfortunately when attempting to do this with a for loop the armadillo matrix will copy the value from the old matrix to the ENTIRE column when the lowest-most element is non-zero (I am using sparse matrices). I have attached the code that is being used below. This is very simplified because I need to figure out why it is doing this first. According to the documentation this should work.
main()
{
OldMatrixClass MatrixA(size, size);
FillMatrix(MatrixA, size);
for (auto i = 0; i < size; i++)
{
for (auto j = 0; j < size; j++)
{
file << MatrixA[i][j] << "\t";
}
file << "\n";
}
arma::Mat<double> ArmaA(size, size);
ArmaA.zeros();
CopyMatrix(MatrixA, ArmaA, size);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
file << ArmaA[i,j] << "\t";
}
file << "\n";
}
}
void FillMatrix(OldMatrixClass &A, int size)
{
double num = 0.15;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (rand() % 101 < 26)
{
A[i][j] = num;
}
else
{
A[i][j] = 0;
}
}
}
}
void CopyMatrix(OldMatrixClass A, arma::Mat<double> &B, int size)
{
for (int k = 0; k < size; k++)
{
for (int j = 0; j < size; j++)
{
B[j, k] = A[j][k];
}
}
}
This is the output when I run this code... As you can see only the entire columns are altered if the last element in MatrixA column is non-zero.
Output
Is there something I'm doing wrong? Is my syntax messed up somewhere?
See the documentation:
Caveat: accessing elements via [i,j] and [i,j,k] does not work correctly in C++; instead use (i,j) and (i,j,k)
I am trying to create a 2d vector of set size and then insert data into it. The issue I am having is being able to insert the data filling up each column and row in the 2d vector.
I have read through various other threads, but cannot find an implementation that works for me.
Here is some sample code for my issue:
int main()
{
vector<string> strVec = { "a","b","c","d" };
// letters to insert into vector
// this is just a sample case
vector< vector<string>> vec; // 2d vector
int cols = 2; // number of columns
int rows = 2; // number of rows
for (int j = 0; j < cols; j++) // inner vec
{
vector<string>temp; // create a temporary vec
for (int o = 0; o < rows; o++) // outer vec
{
temp.push_back("x"); // insert temporary value
}
vec.push_back(temp); // push back temp vec into 2d vec
}
// change each value in the 2d vector to one
// in the vector of strings
// (this doesn't work)
// it only changes the values to the last value of the
// vector of strings
for (auto &v : strVec)
{
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
vec[i][j] = v;
}
}
}
// print 2d vec
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j];
}
cout << endl;
}
}
You are assigning same string to all elements of vec again and again in the loop for (auto &v : strVec).
That is, vec[0][0]=vec[0][1]=vec[1][0]=vec[1][1]=a, vec[0][0]=vec[0][1]=vec[1][0]=vec[1][1]=b, and so on.
Removing this outer loop and assigning strVec[i*cols+j] to vec[i][j], we can get desired output.
DEMO is here.
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
vec[i][j] = strVec[i*cols+j];
}
}
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < 2; j++)
{
cout << vec[i][j];
}
cout << endl;
}
I'm trying to change a 2D array values in different function so it suits my need.
For an example in the code below I'm trying to change the Matrix to reflexive by changing the to "1" .
I have an issue returning the new array and replace it with the old one in the main program so I can use it again.
int reflexive(int m, int n, int matrix[100][100])
{
for(int i = 1;i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
if(i == j)
{
if(matrix[i][j] != 1)
matrix[i][j] = 1;
}
return matrix;
}
}
}
int main()
{
int matrix[100][100];
int m , n;
for(int i = 1;i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
cin>>matrix[i][j];
}
}
matrix[m][n] = reflexive(m,n,matrix);
return 0;
}
I want to fix this code so it can return the full new 2D array to the main program.
In your function reflexive the return statement is inside the loop. This causes that the function is terminated immediately when the return statement is reached. If you would proper format your code you would be able to see this behavior easily.
Further note, that the first element in an array has the index 0 and the index of the last element is the the size of the array -1. This is, because the array index is the element "offset". This means that
a[i]
and
*(a + i)
access the same array element.
for(int i = 1;i <= m; i++) // <--- should be for(int i=0; i<m; i++)
{
for(int j = 1; j <= n; j++) // <--- should be for(int j=0; j<n; j++)
{
if(i == j)
{
if(matrix[i][j] != 1)
matrix[i][j] = 1;
}
return matrix; // <--- here the function is terminated
}
}
An array is passed to a function by a pointer and not by value. This means if a formal parameter of a function is an array, then not all the values of the actual parameter are copied to the array. There is only passed a pointer to the values of the paramter to the function.
Because of that you do not need any return value in your case.
The following code should do what you want:
int reflexive(int m, int n, int matrix[100][100])
{
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
if(i == j)
{
if(matrix[i][j] != 1)
matrix[i][j] = 1; // <----- write to the data by pointer
}
}
}
}
int main()
{
int matrix[100][100];
......
reflexive(m,n,matrix);
......
}
Use vector<vector<int>> and pass it by reference
Using an array here is tedious and error-prone. Therefore:
#include <vector>
std::vector<std::vector<int> > reflexive(std::vector<std::vector<int> > matrix)
{
for(int i = 0; i < matrix.size; i++){
for(int j = 0; j < matrix[i].size; j++){
....
int main()
{
std::vector<std::vector<int> > matrix(100, std::vector<int>(100));
int m , n; //<<whereever they come from I guess? Saw no cin in your code. Have no set value right now.
for(int i = 0; i < m; i++)
{
for(int j =0; j < n; j++)
{
cin>>matrix[i][j];
}
}
}
or
#include<array>
...
std::array<std::array<int, 100>, 100> matrix;
std::array is of fixed size, std::vector is dynamic. Also learn about std::list while you're at it.
In any case, you should find a basic tutorial that talks about them and read all of it. Important standards.
That said, especially if you want to use dynamic sized arrays, you have no guarantee that every sub-array is of the same size ("a jagged 2d array), which is somehwat okay in a small program but should be avoided in the big picture, by using a class that ensures this property.
In the case of a matrix, one easy option is to use a library that deals with matrices, like for example Eigen.
edit:
If at some point you need the code above in const-correct:
std::vector<std::vector<int> > reflexive(const std::vector<std::vector<int> >& input)
{
std::vector<std::vector<int> > output = input;
....
return output;
}
(or make it void if it is allowed to modify the original matrix)
i'm trying to use a matrix of vectors declared like this :
vector<vector<Neurone*>* > Neurones;
I have already created a class Neurones by the way.
this is the code :
NeuralNetwork::NeuralNetwork(vector<int> NeuroneNumbers, vector<vector<vector<double>* >* > lw)
{
for (int i = 0; i < NeuroneNumbers.size(); i++)
{
if (i == 0)
{
Neurones.push_back(new vector<Neurone*>());
for (int j = 0; j < NeuroneNumbers[i]; j++)
{
Neurones[i]->push_back(new Neurone(new Neurone[0], new double[0]));
if (j == NeuroneNumbers[i] - 1)
{
(*Neurones[i])[j]->Value = 1;//Here is the error ! with i=0 j=0 segmentation fault !
}
}
}}
A matrix of std::vector is actually a std::vector of std::vector.
Here is an example:
#include <iostream>
#include <vector>
int main() {
// this is actually a 2D "array"
std::vector< std::vector<int> > v;
int N = 5; // rows
int M = 5; // columns
v.resize(N); // create N rows
for(int i = 0 ; i < N ; ++i)
for(int j = 0 ; j < M ; ++j)
v[i].push_back(j); // fill the matrix
//print
for(int i = 0 ; i < N ; ++i)
for(int j = 0 ; j < M ; ++j)
std::cout << v[i][j] << "\n";
return 0;
}
[EDIT]
I believe, that you do need to use pointers for your purpose.
Neurones[i]->push_back(new Neurone(new Neurone[0], new double[0]));
Why are you doing this? What does new Neurone[0] means? If you meant to create a Neurone by this statement, then actually you are passing one Neurone object to the constructor of another.
Neurones[i])[j] will give you a pointer to Neuron. So if Neuron class has a public member variable named Value, you can set it by
Neurones[i])[j]->Value = 1; // no need the * operator
However, you are using pointers unnecessarily, and the usage is error prone. Your code can be greatly simplified:
vector<vector<Neurone*> > Neurones;
NeuralNetwork::NeuralNetwork(vector<int> NeuroneNumbers, vector<vector<vector<double>* >* > lw)
{
for (int i = 0; i < NeuroneNumbers.size(); i++)
{
if (i == 0)
{
vector<Neurone*> neuronVector;
for (int j = 0; j < NeuroneNumbers[i]; j++)
{
Neurone neuron=new Neurone(/*constructor params*/); // create a Neuron object
if (j == NeuroneNumbers[i] - 1)
{
neuron.Value = 1;
}
neuronVector.push_back(neuron);
}
Neurones.push_back(neuronVector);
}}
I keep getting the error Heap Corruption Detected. I have read through several questions on here, but I can't quite find out what is causing this in my code. I am trying to create a 2d array that will hold a matrix that is read from a text file.
// Create a 2d matrix to hold the matrix (i = rows, j = columns)
matrix = new int*[cols];
for(int i = 0; i <= cols; i++) {
matrix[i] = new int[rows];
}
// Populate the matrix from the text file
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
inputFile >> matrix[i][j];
}
}
My destructor is:
for(int i = 0; i <= cols; i++) {
delete[] matrix[i];
}
delete[] matrix;
I've tried debugging, but that does do much help in this case. Any suggestions?
matrix = new int*[cols];
for(int i = 0; i <= cols; i++) {
matrix[i] = new int[rows];
}
For an array with cols elements, the index is from 0 to cols - 1 inclusively.
The heap corruption will be detected when
delete [] matrix;
Since matrix[cols] write a position out of array bound.
UPDATE
As #DanielKO (thank you buddy :p) pointed out in the comment
there is a mismatch, the "Populate the matrix..." loop makes "i"
iterate over "rows" when it should be iterating over "cols".
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
inputFile >> matrix[i][j];
When you allocated you went from 0 to cols in i. Now you're changing i to be rows.
EDIT: Below would honor your commented row/column rules and follow RAII:
std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols));
for( int i=0; i<rows; ++i ) {
for( int j=0; j<cols; ++j ) {
inputFile >> matrix[i][j];
}
}
// no need for delete matrix cleaned up when leaving scope.