Безопасно ли таким образом создавать динамический массив?
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int **createArray(int rows, int cols) {
int **arr = new int*[rows];
for (int i = 0; i < rows; i++)
arr[i] = new int[cols];
return arr;
}
void setArray(int **arr, int rows, int cols) {
srand(time(0));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = 1 + rand() % 10;
}
}
}
void printArray(int **arr, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << setw(3) << left << arr[i][j] << " ";
}
cout << endl;
}
}
int main() {
setlocale(LC_ALL, "");
int rows, cols;
cout << "Введите кол-во строк: ";
cin >> rows;
cout << "Введите кол-во столбцов: ";
cin >> cols;
int **arr = createArray(rows, cols);
setArray(arr, rows, cols);
printArray(arr, rows, cols);
_getch();
return 0;
}
Безопасно ли (с точки зрения утечки памяти) использовать функцию createArray?