Data cycles
for (int i = 0; i < refm; i++) { for (int j = 0; j < refn; j++) { ^^^^^^^^ *(*(ar1 + i) + j) = *(*(ar + i) + j); } }
lead to indefinite program behavior, as in the sentence
*(*(ar1 + i) + j) = *(*(ar + i) + j);
there is an attempt to refer to the non-existent elements of the array ar , which has refn - 1 columns.
The same problem exists for the cycle.
for (int j = 0; j < refn; j++) { *(*(ar1 + j) + refn-1) = 9; ^^^^^^^ }
since the ar1 array has refm lines, not refn lines.
Also, you do not delete all the memory allocated for the original array, which leads to a memory leak.
Below is a demonstration program that shows how this function can be defined. I called it appendArray
#include <iostream> #include <algorithm> void freeArray( int ** &a, size_t m ) { for ( size_t i = 0; i < m; i++ ) delete [] a[i]; delete [] a; a = nullptr; } bool appendColumn( int ** &a, size_t m, size_t n, int value ) { int ** tmp = new int *[ m ]; for ( size_t i = 0; i < m; i++ ) tmp[i] = new int[ n + 1 ]; for ( size_t i = 0; i < m; i++ ) { std::copy( a[i], a[i] + n, tmp[i] ); } for ( size_t i = 0; i < m; i++ ) tmp[i][n] = value; freeArray( a, m ); a = tmp; return true; } int main() { size_t m = 2; size_t n = 3; int **a; a = new int *[m]; a[0] = new int[n] { 1, 2, 3 }; a[1] = new int[n] { 4, 5, 6 }; for ( size_t i = 0; i < m; i++ ) { for ( size_t j = 0; j < n; j++ ) std::cout << a[i][j] << ' '; std::cout << std::endl; } n += appendColumn( a, m, n, 9 ); for ( size_t i = 0; i < m; i++ ) { for ( size_t j = 0; j < n; j++ ) std::cout << a[i][j] << ' '; std::cout << std::endl; } freeArray( a, m ); return 0; }
Her console output is as follows.
1 2 3 4 5 6 1 2 3 9 4 5 6 9