It compiles and I can assign a value to the first element in the array and output one, but I cannot assign anything to the second element and it crashes when I try to output it. Any help would be appreciated.
I have to do a whole mess of other functions to go along with this but I cant even get the base working, its driving me mad!
Ah okay, thank you very much, I also have a follow up question. I'm trying fill that array with int to pointers, so I would have that array filled with other arrays to create a dynamic 2d array. What I have tried isn't working because of the type mismatch int * to int but I'm not sure how to go about it.
1 2
for (int i = 0; i <= column; i++)
*(**s + i) = newint [column];
*(**s + i)Accesses a specific int (you have three levels of indirection and three dereferences, so in the end you have int). Compare
1 2 3
int* i = nullptr;
*i = newint[100]//Incorrect
i = newint[100]; //Correct
So you need to drop outside dereference. Also your addition in wrong place: you meant to change second-level pointer, which points to arrays of ints, but changing third level one, which points to individual ints. Correct code: *(*s + i) = newint [column];
Note that *(x + y) is equal to x[y]. So it would be better to use *s[i] = newint [column]; as it conveys that it is an array of arrays. If you will make your top-level pointer an array too (3D array), then it would be even better to use this: s[0][i] = newint [column];
int ***s = newint **;
*s = newint*;
**s = newint[row];
for (int i = 0; i <= column; i++)
(*s)[i] = newint[column];
This is what I have, and it compiles. On line three its creating the rows array and the for loop is filling that array with column arrays correct? So (*s)[i] is accessing rows elements and
(*s)[0][1];
Would be accessing rows base and the second element in columns?
This is memory leak. **s is (*s)[0]
You are assigning to that pointer at frst iteration again. In addition to that, *s is a single int pointer and not an array pointer, so you have buffer overrun here.
Correct way:
1 2 3 4 5 6 7 8
int*** s = newint**; //Assuming you do not want 3D array
*s = newint*[rows];
for (int i = 0; i < rows; ++i) //Note < and rows
(*s)[i] = newint[columns];
//Access
for (int row = 0; row < rows; ++i)
for (int column = 0; column < columns; ++i)
(*s)[row][column] = 0; //Set all values to 0
// Make a 1 dimensional array with "size1" elements
int *make1D(size_t size1)
{
returnnewint[size1];
}
// Make a 2D array
int **make2D(size_t size2, size_t size1)
{
// 1st dimension is array of pointers to 1D arrays
int **result = newint*[size2];
// Now fill in each element of 1st dimension with a 1D array
for (size_t i=0; i<size2; ++i) {
result[i] = make1D(size1);
}
return result;
}
// Make a 3D array
int ***make3D(size_t size3, size_t size2, size_t size1)
{
// 1st dimension is pointers to pointers to 2D arrays
int ***result = newint**[size3];
// Now fill in each element with a 2D array
for (size_t i=0; i<size3; ++i) {
result[i] = make2D(size2, size1);
}
return result;
}
Does last s should point to first element to the array (act as array pointer) or point to actual array pointer? Because in later case you would need an extra level of indirection.
In this case you need to allocate memory for *s and store it in s, then allocate memory for **s and store pointer to it in *s, and then you need to allocate memory for whole array of int and store pointer to it in **s
int*** new_array( std::size_t x, std::size_t y, std::size_t z )
{
int* array = newint[x*y*z] {} ; // one-dimensional array of x*y*z int
int** array2 = newint* [x*y] {} ; // one-dimensional array of x*y pointers to int
for( std::size_t i = 0 ; i < x*y ; ++i ) array2[i] = array + i*z ;
// this gives us a view of array as a two-dimensional array
// eg. array2[i][j] yields array[ i*z + j ]
int*** array3 = newint** [x] ; // one-dimensional array of x pointers to pointers to int
for( std::size_t i = 0 ; i < x ; ++i ) array3[i] = array2 + i*y ;
// this gives us a view of array as a three-dimensional array
// eg. array3[i][j][k] yields array2[ i*y + j ][k] ie. array[ ( i*y + j ) * z + k ]
return array3 ;
}
void delete_array( int*** array3 )
{
delete[] array3[0][0] ; // delete one-dimensional array of int
delete[] array3[0] ; // delete one-dimensional array of pointers to int
delete[] array3 ; // delete one-dimensional array of pointers to pointers to int
}
Ah, so you do need additional level of indirection. In this case s should be int**** s
Other than that, everything like earlier, but with additional level of indirection added. If you are allowed to, use keskiverto approach without dynamic allocations (or at last minimise them: only array should be in dynamically allocated)