1

In this C++ code sizeof (ar) doesn't help me to find cols and rows variables and always gives me the same wrong cols and rows. How can I find the size of the matrix without passing sizeX and sizeY variables to the IsMagicSquare(arr) function? Can you help me to understand this problem and find a way to solve it?

int main()
{
    int sizeX, sizeY;
    cout << "Size of Matrix:";
    cin >> sizeX >> sizeY;

    int** arr = new int* [sizeY];
    for (int i = 0; i < sizeY; ++i)
    {
        arr[i] = new int[sizeX];
    }

    cout << "Elements of the Matrix:";
    for (int i = 0; i < sizeX; i++)
        for (int j = 0; j < sizeY; j++)
            cin >> arr[i][j];

    if (IsMagicSquare(arr))
    {
        for (int i = 0; i < sizeX; i++)
        {
            cout << "\n";
            for (int j = 0; j < sizeY; j++)
                cout << arr[i][j];
        }
    }
    else
    {
        for (int i = 0; i < sizeY; i++)
        {
            delete[] arr[i];
        }
        delete[] arr;
    }


    return 0;
}

bool IsMagicSquare(int** ar)
{
    int rows = sizeof (ar)/ sizeof (ar[0]);
    int cols = sizeof (ar[0]) / sizeof(int); 
    cout << rows << cols;
    if (rows == cols)
    {
        int* ver = new int[rows]();
        int* hor = new int[cols]();
        int cross0=0;
        int cross1=0;

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                hor[i] += ar[i][j];
                ver[j] += ar[i][j];
                if (i == j)
                    cross0 += ar[i][j];
                else if ((i+j) == cols)
                    cross1 += ar[i][j];
            }
        }
        if (cross0 != cross1)
            return false;
        else
        {
            for (int i = 0; i < rows; i++)
                if ((cross0 != ver[i]) || (cross1 != hor[i]))
                    return false;
        }
    }
    else
        return false;

    return true;
}
JohnC
  • 2,687
  • 1
  • 22
  • 30
Em Rul
  • 57
  • 5
  • 4
    `int** arr = new int* [sizeY];` -> `std::vector> arr(sizeX, std::vector(sizeY))`. – Jarod42 Dec 25 '19 at 19:44
  • 1
    The `sizeof` of a pointer is the size of the pointer itself, not what it might point to. – Some programmer dude Dec 25 '19 at 19:45
  • `sizeof (ar)/ sizeof (ar[0])` is actually `sizeof (int**)/ sizeof (int*)` so probably 1. – Jarod42 Dec 25 '19 at 19:46
  • 2
    Stop using C-style arrays. Use `std::array` or `std::vector` instead, so you operate on objects that *know* their `.size()`, rather than dumb C arrays that decay to pointers and lose all information. – Jesper Juhl Dec 25 '19 at 19:46
  • Also stop with the manual memory management (`new`/`delete`). Use containers or smart pointers. It's not the 1990's any more.. – Jesper Juhl Dec 25 '19 at 19:49
  • [Simple and effective matrix class](https://stackoverflow.com/a/2076668/4581301). Add getters for `mRows` and `mCols` and you're good to go. – user4581301 Dec 25 '19 at 19:54
  • Jesper Juhl, I jumped to C++ from C two weeks ago. Should I always use std array or vector from now on and forget about all thing in C, what is the best way? – Em Rul Dec 25 '19 at 19:55
  • 1
    Don't forget the C Array, but reserve its use for edge cases, building blocks in more complicated structures, or really simple stuff where its drawbacks can't hurt you. – user4581301 Dec 25 '19 at 19:57
  • 1
    Side note: The guiding idioms of C++ are significantly different from those of C. Quite a few years ago I added C++ to the programming tools I used and treated it too much like C for a couple years. I'm still finding and fixing bits of code from that era. I recommend keeping this period of your life as short as possible by getting some reference materials from the [Intermediate and Best Practices sections](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that focus more on the ideology than the nuts and bolts and teach yourself to think in C++. – user4581301 Dec 25 '19 at 20:11
  • Does this answer your question? [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) – ChrisMM Dec 26 '19 at 00:26

1 Answers1

1

Just sum the allocations you are making for a reasonable estimate:

  • The outer array: sizeY * sizeof(int*).
  • The inner arrays: sizeY * sizeX * sizeof(int)

Of course, the size calculations you got in IsMagicSquare() won’t work: sizeof operates on the types rather than the actual allocated memory. That information is lost and can’t be recovered from the pointers. You are best off to use, e.g., a std::vector<std::vector<int>>. That deals with automatic memory allocation and it tracks the sizes of the structure.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380