Другие языки программирования и технологии

Код c++ выдает ошибку

Значит, решил я сделать трехмерный динамический массив, естественно через указатели. Выдает ошибку "Вызвано исключение по адресу ". Помогите, кто знает, пожалуйста.

Вот код:

#include
using namespace std;
void PrintArray(int*** FirstArray, const int ROW, const int COL, const int ZED)
{
FirstArray = new int** [ROW];
for (int i = 0; i < ROW; i++)
{
FirstArray[i] = new int* [COL];
for (int g = 0; g < COL; g++)
{
FirstArray[i][g] = new int[ZED];
}
}

for (int i = 0; i < ROW; i++)
{
for (int g = 0; g < COL; g++)
{
for (int z = 0; z < ZED; z++)
{
FirstArray[i][g][z] = rand() % 10;

}
}
}

for (int i = 0; i < ROW; i++)
{
delete[] FirstArray[i];
for (int g = 0; g < COL; g++)
{
delete[] FirstArray[i][g];
for (int z = 0; z < ZED; z++)
{
delete[] FirstArray[g][z];
}
}
}
delete[] FirstArray;
}

void ShowArray(int*** FirstArray, const int ROW, const int COL, const int ZED)
{
for (int i = 0; i < ROW; i++)
{
for (int g = 0; g < COL; g++)
{
for (int z = 0; z < ZED; z++)
{
cout << FirstArray[i][g][z] << "\t";
}
}
}
}

int main()
{
int const ROW = 10;
int const COL = 3;
int const ZED = 2;
int*** arr[ROW][COL][ZED];
PrintArray(***arr, ROW, COL, ZED);
ShowArray(***arr, ROW, COL, ZED);
}

Ошибка по моему где то в конце в строках
int*** arr[ROW][COL][ZED];
PrintArray(***arr, ROW, COL, ZED);
ShowArray(***arr, ROW, COL, ZED);
Не пойму как правильно объявить массив, Чтобы потом работала с ним функция PrintArray
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
int*** create(const size_t x, const size_t y, const size_t z) {
auto cube = new(nothrow) int** [x];
if (cube != nullptr) {
size_t i, j;
for (i = 0; i < x; ++i) {
cube[i] = new(nothrow) int* [y];
if (cube[i] == nullptr) {
for (size_t k = 0; k < i; ++k) {
for (size_t m = 0; m < y; ++m) delete[] cube[k][m];
delete[] cube[k];
}
delete[] cube;
cube = nullptr;
break;
}
for (j = 0; j < y; ++j) {
cube[i][j] = new(nothrow) int[z];
if (cube[i][j] == nullptr) {
for (size_t k = 0; k < j; ++k) delete[] cube[i][k];
for (size_t k = 0; k < i; ++k) {
for (j = 0; j < y; ++j) delete[] cube[k][j];
delete[] cube[k];
}
delete[] cube;
cube = nullptr;
break;
}
}
if (cube == nullptr) break;
}
}
return cube;
}
int*** destroy(int*** cube, const size_t x, const size_t y) {
if (cube != nullptr) {
for (size_t i = 0; i < x; ++i) {
for (size_t j = 0; j < y; ++j) delete[] cube[i][j];
delete[] cube[i];
}
delete[] cube;
cube = nullptr;
}
return cube;
}
void fill(int*** cube, const size_t x, const size_t y, const size_t z) {
if (cube != nullptr) {
uniform_int_distribution<> uid(-100, 100);
mt19937 gen{ random_device()() };
for (size_t i = 0; i < x; ++i)
for (size_t j = 0; j < y; ++j)
for (size_t k = 0; k < z; ++k)
cube[i][j][k] = uid(gen);
}
}
void print(int*** cube, const size_t x, const size_t y, const size_t z) {
if (cube != nullptr) {
for (size_t i = 0; i < x; ++i) {
for (size_t j = 0; j < y; ++j) {
for (size_t k = 0; k < z; ++k) {
cout
<< "[" << i << "][" << j << "][" << k << "] ="
<< setw(5)<< cube[i][j][k] << '\n';
}
cout.put('\n');
}
cout.put('\n');
}
}
}
int main() {
auto x = 10U, y = 3U, z = 2U;
auto cube = create(x, y, z);
fill(cube, x, y, z);
print(cube, x, y, z);
cube = destroy(cube, x, y);
system("pause > nul");
}
Kirill Fadeev
Kirill Fadeev
85 260
Лучший ответ
Тут
int*** arr[ROW][COL][ZED];
шестимерный массив

В функции PrintArray массив распределяет память, заполняется и память освобождается, после чего в функции ShowArray пытаешься "показать" массив, но он уже "удален"
Siyovush Atajanov
Siyovush Atajanov
68 825
int*** arr = new int**[ROW];
for (int i = 0; i < ROW; i++) {
arr[i] = new int*[COL];
for (int j = 0; j < COL; j++) {
arr[i][j] = new int[ZED];
}
}

и в конце

for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
delete[] arr[i][j];
}
delete[] arr[i];
}
delete[] arr;
Тёма ....
Тёма ....
51 164
Расим Кязимов Все равно эта же ошибка вылетает