S
S
soqrave2020-02-16 19:20:28
C++ / C#
soqrave, 2020-02-16 19:20:28

How to correctly pass a two-dimensional dynamic array to a function?

The error occurs 0xC0000005 when the function works at the moment of assigning a value to a cell of an array.

#include <cstdlib>
#include <iostream>

using namespace std;




void Foo(int** matrix, int const rows, int const cols)
{
  for (int i = 0; i < rows; i++)
  {
    cout << "\n"; 
    for (int j = 0; j < cols; j++)
    {
      matrix[i][j] = 1;
      cout << "\t";
      
    }
  }
}

int main()
{
  int rows = 0;
  int cols = 0;
  cout << "Введите столбцы - ";
  cin >> rows;
  cout << "Введите строки - ";
  cin >> cols;
  int** pmatr = new int* [rows];
  Foo(pmatr, rows, cols);

  for (int i = 0; 0 < rows; i++)
      delete[] pmatr[i];
  delete[] pmatr;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2020-02-16
@soqrave

In C++, a two-dimensional dynamic array is passed to a function like this: And masturbation with arrays of pointers is spaghetti and guaranteed leaks and segfaults.
void Foo(std::vector< std::vector <int> > &matrix)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question