can you help me change the size of dynamic allocated 2D array. I need function witch change current array size for required size. I send my code for example. Thanks
int main()
{
//create array col x row
int **array = new int*[col];
for (int i = 0; i < col; i++)
{
array [i] = new int[row];
}
//add numbers to array
for (int i = 0; i < col; i++)
{
for (int j = 0; j < row; j++)
{
pole[i][j] = i*j;
}
}
/*
call function for change size of matrix ...
for example row to rov+5 and col to col+1 without loss of data
*/
//free memory
for (int i = 0; i < col; i++)
{
delete[] pole[i];
}
delete pole;
}
void resize(int ** & arr)
{
//code
}
I am not sure what do you want to accomplish here. Probably the easiest way is to copy the old matrix into a larger one, before deleting it. If you want to really reallocate the memory, and avoid more complex structures that c++ provides, you could use #include<cstdlib>,and realloc() all needed rows and columns. But that is not considered c++ style.
More info: http://www.cplusplus.com/reference/cstdlib/realloc/
This is untested but will hopefully give you some ideas;
void create(int ** & arr)
{
//create array col x row
arr = malloc(col * sizeof(int*));
for (int i = 0; i < col; i++)
{
arr [i] = malloc(row * sizeof(int));
}
}
void destroy(int ** & arr)
{
for (int i = 0; i < col; i++)
{
free(arr [i]);
}
free(arr);
}
void resize(int ** & arr,int new_col,int new_row)
{
if (new_col == col)
{
if (new_row == row)
return;
for (int i = 0; i < col; i++)
{
arr [i] = realloc(arr [i],new_row * sizeof(int));
}
}
else if (new_col > col)
{
arr = realloc(arr,new_col * sizeof(int*));
if (new_row != row)
{
for (int i = 0; i < col; i++)
{
arr [i] = realloc(arr [i],new_row * sizeof(int));
}
}
for (int i = col; i < new_col; i++)
{
arr [i] = malloc(arr [i],new_row * sizeof(int));
}
}
else
{
if (new_row != row)
{
for (int i = 0; i < new_col; i++)
{
arr [i] = realloc(arr [i],new_row * sizeof(int));
}
}
for (int i = new_col; i < col; i++)
{
free(arr [i]);
}
arr = realloc(arr,new_col * sizeof(int*));
}
col = new_col;
row = new_row;
}
I would create a matrix having the needed size, the correct number of rows and the correct number of columns, copy the content of the old matrix and do whatever is needed to do with the new rows and new columns and then deallocate the old matrix.
Related
So I got a function which creates me 2D array and fill it with test data.
Now I need to assign the pointer to an array
//Fill matrix with test data
int *testArrData(int m, int n){
int arr[n][m];
int* ptr;
ptr = &arr[0][0];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
*((ptr+i*n)+j) = rand()%10;
}
}
return (int *) arr;
}
int arr[m][n];
//Algorithm - transpose
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
arrT[j][i] = arr[i][j];
}
}
Is there any way of doing this?
There are at least four problems with the function.
//Fill matrix with test data
int *testArrData(int m, int n){
int arr[n][m];
int* ptr;
ptr = &arr[0][0];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
*((ptr+i*n)+j) = rand()%10;
}
}
return (int *) arr;
}
First of all you declared a variable length array
int arr[n][m];
Variable length arrays are not a standard C++ feature.
The second problem is that these for loops
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
*((ptr+i*n)+j) = rand()%10;
}
}
are incorrect. It seems you mean
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
*((ptr+i*m)+j) = rand()%10;
}
}
You are returning a pointer to a local array with automatic storage duration that will not be alive after exiting the function. So the returned pointer will be invalid.
And arrays do not have the assignment operator.
Instead use the vector std::vector<std::vector<int>>. For example
std::vector<std::vector<int>> testArrData(int m, int n){
std::vector<std::vector<int>> v( n, std::vector<int>( m ) );
for ( auto &row : v )
{
for ( auto &item : row )
{
item = rand() % 10;
}
}
return v;
}
This is how I would accomplish this. I agree with int ** because it is easy to understand if you dont know how to use vectors. Also, the rand() can cause trouble if you are using the result to index an array. Make sure to use abs(rand() % number) if you don't want negative numbers.
I've updated the answer due to some vital missing code.
// This method creates the overhead / an array of pointers for each matrix
typedef int* matrix_cells;
int **create_row_col_matrix(int num_rows, int num_cols, bool init_rnd)
{
num_rows = min(max(num_rows, 1), 1000); // ensure num_rows = 1 - 1000
num_cols = min(max(num_cols, 1), 1000); // ensure num_cols = 1 - 1000
int *matrix_total = new int[num_rows*num_cols];
// overhead: create an array that points to each row
int **martix_row_col = new matrix_cells[num_rows];
// initialize the row pointers
for (int a = 0; a < num_rows; ++a)
{
// initialize the array of row pointers
matrix_row_col[a] = &matrix_total[num_cols*a];
}
// assign the test data
if (init_rnd)
{
for (int run_y = 0; run_y < num_rows; ++run_y)
{
for (int run_x = 0; run_x < num_cols; ++run_x)
{
matrix_row_col[run_y][run_x] = abs(rand() % 10);
}
}
}
return matrix_row_col;
}
int src_x = 7, dst_x = 11;
int src_y = 11, dst_y = 7;
int **arr_src = create_row_col_matrix(src_y, src_x, true);
int **arr_dst = create_row_col_matrix(dst_y, dst_x, false);
for (int a = 0; a < dst_y; ++a)
{
for (int b = 0; b < dst_x; ++b)
{
arr_dst[a][b] = arr_src[b][a];
}
}
delete matrix_src[0]; // int *matrix_total = new int[src_y*src_x]
delete matrix_src; // int **matrix_row_col = new matrix_cell[src_y]
delete matrix_dst[0]; // int *matrix_total = new int[dst_y*dst_x]
delete matrix_dst; // int **matrix_row_col = new matrix_cell[dst_y]
// the overhead is matrix_src and matrix_dst which are arrays of row pointers
// the row pointers makes it convenient to address the cells as [rown][coln]
I am trying to define a 2D array, but I want to do it in a function,
here is my code:
int** createArray( int columns, int rows)
{
int** array[rows];
for(int i = 0; i < rows; i++)
{
array[i] = new int*[columns];
}
for(int i = 0; i <columns; i++)
{
for(int j = 0; j < rows; j++)
{
array[i][j] = 0;
std::cout <<array[i][j];
}
std::cout<<"\n";
}
return *array;
}
int main()
{
int **myArray = createArray(3,5);
for(int k =0; k < 5; k++)
{
if( (myArray[0][k] == 0) && (&myArray[1][k] == 0)) //segmentation fault
{
myArray[2][k] = 10; //segmentation fault
}
delete[] myArray;
}
But it causes errors which can be seen as comments in lines. I am new to C++ and I do not know how to fix this.
Thank you very much
Prefer std::vector over manual memory management:
std::vector<std::vector<int>> createArray(int columns, int rows)
{
return std::vector<std::vector<int>(rows, std::vector<int>(columns));
}
int main()
{
int COLUMNS = 5;
int ROWS = 3;
auto myArray= createArray(COLUMNS, ROWS);
/*
Do stuff
*/
//std::vector handles delete on it's own, no need to clean up
}
If you cannot use std::vector for some reason, this is the a way to initialize 2D array on the heap:
int** createArray(int columns, int rows)
{
int** arr = new int*[rows];
for(int i = 0; i < rows; ++i)
{
arr[i] = new int[columns];
}
return arr;
}
int main()
{
int COLUMNS = 5;
int ROWS = 3;
int** myArray= createArray(COLUMNS, ROWS);
/*
Do stuff
*/
//you need to a delete for every new and delete[] for every new[]
for(int i = 0; i < rows; ++i)
{
delete[] myArray[i];
}
delete[] myArray;
}
I'm trying to run through a 2-dimensional array and update values using a pointer to pointer to int.
Swap function:
void foo(int ** vacancies, int **transfers, int transfer)
{
for (int i = 0; i < transfer; i++)
{
(*transfers[i]) = 0;
(*vacancies[i]) = 2;
}
}
Declaration:
int ** vacancies = new int*[getVacancies(grid)];
int ** transfers = new int*[transfer];
Function call:
foo(vacancies, transfers, transfer);
Unfortunately this doesn't seem to actually update any values, is there something I need to change? Thanks!
Edit:
getVacancies(vacancies, grid, transfer);
getTransfers(transfers, grid, transfer);
void getVacancies(int ** vacancies, int grid[][ROW], int vCount)
{
for (int i = 0; i < vCount; i++)
{
for (int row = 0; row < ROW; row++)
{
for (int col = 0; col < COL; col++)
{
if (grid[col][row] == 0)
{
vacancies[i] = &grid[col][row];
}
}
}
}
}
And the same for getTransfers.
Edit 2:
void getVacancies(int ** vacancies, int grid[][ROW], int vCount)
{
int i = 0;
for (int row = 0; row < ROW; row++)
{
for (int col = 0; col < COL; col++)
{
if (grid[col][row] == 0)
{
vacancies[i] = &grid[col][row];
i++;
}
}
}
}
You have allocated only "one dimension". Those int* elements should point to arrays of int or just ints. Dereferencing these uninitialized pointers is undefined behavior.
I think this is how you need to initialize your array. You shouldn't loop through vacancies, because that will fill each element with a pointer to the same element of grid (the last vacant one). Instead, you just want to loop through grid, and add each vacant element to the next entry in vacancies.
I've also changed the function to return the number of elements that were filled in. Alternatively, you could initialize vacancies to nullptr in each element, and test for this when looping through it later.
int getVacancies(int ** vacancies, int grid[][ROW], int vCount)
{
int i = 0;
for (int row = 0; row < ROW; row++)
{
for (int col = 0; col < COL; col++)
{
if (grid[col][row] == 0)
{
if (i >= vCount) { // Prevent overflowing vacancies
return i;
}
vacancies[i++] = &grid[col][row];
}
}
}
return i;
}
Allocate a two dimensional array like this (see How do I declare a 2d array in C++ using new?):
int** twoDimensionalArray = new int*[rowCount];
for (int i = 0; i < rowCount; ++i) {
twoDimensionalArray[i] = new int[colCount];
}
I have a float** array that contains num_rows rows and num_cols columns. I'd like to determine the number of occurrences of every number between 0-9 columnwise. To do this, I thought of using another 2D array of size [10][num_cols], so that for each column the number corresponding to an element is the number of occurrences of that number in the original table.
Example: if the original table contains 1 2 3 1 1 in the fifth column, then in the second column, the values should be like: 1-> 3, 2 -> 1, 3 -> 1.
I tried using the function as follows, but it gives me a pointer error. I tried using vectors but that too brings no luck.
int ** attribute_count(float * * input, int row_num, int col_num) {
int ** arr_2 = new int * [10];
int * arr = new int[10 * col_num];
int counter = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < col_num; j++) {
arr_2[i][j] = 0;
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < col_num; j++) {
int temp = input[i][j];
arr_2[temp][j]++;
}
}
return arr_2;
}
EDIT:
I tried your suggestions. The new code is:
int** attribute_count(float** input, int row_num, int col_num) {
int** arr_2 = new int* [10];
int* arr = new int[10 * col_num];
int counter = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < col_num; j++) {
arr_2[i] = new int[col_num];
}
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < col_num; j++) {
int temp = input[i][j];
arr_2[temp][j]++;
}
}
return arr_2;
}
This still gives me memory errors. The function is being called in the .cpp like this:
int** attr = attribute_count(training_data, 10, num_cols_train);
cout<<attr[5][1];
Any idea what I'm doing wrong even now?
I think your problem is in incorrect allocation of the 2D array. Try
int ** arr_2 = new int* [row_num];
for (int i = 0; i < row_num; i++)
arr_2[i] = new int[col_num];
You've only allocated one dimension of arr_2. You need to loop through and allocate an array of ints on each one to get the 2nd dimension.
EDIT: Also, what's up with arr? You allocate it, never use it, don't return it, and don't deallocate it. That's how we spell memory leak.
arr_2 is defined and allocated as an array of pointers to int, but you don't actually assign/allocate those pointers.
Here's a stab at correcting your code - however I'm not convinced you have rows and columns the right way around...
int ** attribute_count(float ** input, int row_num, int col_num)
{
int ** arr_2 = new int * [10];
for (int i = 0; i < 10; i++)
{
arr_2[i] = new int[col_num];
for(int j = 0 ; j < col_num ; j++)
{
arr_2[i][j] = 0;
}
}
for (int i = 0; i < row_num; i++)
{
for (int j = 0; j < col_num; j++)
{
int temp = input[i][j];
arr_2[temp][j]++;
}
}
return arr_2;
}
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';
}