Eric Jubber
Eric Jubber

Reputation: 141

Initializing an adjacency matrix in c++

I'm working on graph implementations in C++ and came across an implementation for an adjacency matrix that mostly made sense to me. The implementation uses an "init" function to initialize the matrix:

void init(int n) {

    numVertex = 0;
    numEdge = 0;

    mark = new int[n]; //initialize mark array
    for (int i = 0; i < numVertex; i++) {
        mark[i] = 0;
    }

    matrix = (int**) new int*[numVertex]; //make matrix
    for (int i = 0; i < numVertex; i++) {
        matrix[i] = new int[numVertex];
    }

    for (int i = 0; i < numVertex; i++) { //mark all matrix cells as false
        for (int j = 0; j < numVertex; j++) {
            matrix[i][j] = 0;
        }
    }
}

The line I'm confused about is:

 matrix = (int**) new int*[numVertex]; //make matrix

What does the (int**) aspect do? Why would I choose to use this instead of matrix = new int**[numVertex];?

Thanks so much!

Upvotes: 0

Views: 749

Answers (3)

sandthorn
sandthorn

Reputation: 2870

If column dimension is fixed, you can use vector of array there.
godbolt
wandbox

#include <vector>
#include <array>
#include <iostream>
#include <iomanip>

template<typename T, int col>
using row_templ = std::array<T,col>;

template<typename T, int col, template <typename,int> typename U = row_templ>
using mat_templ = std::vector<U<T,col>>;

int main()
{
    constexpr int numVertex = 30;
    constexpr int numEdge = 30;
    constexpr int numCol = numVertex;
    int numRow = numEdge;
    using row_t = row_templ<int, numCol>; // alias to the explicit class template specialization
    using mat_t = mat_templ<int, numCol>;
    auto make_mat = [&](){ return mat_t(numRow); }; // define a maker if lazy

    mat_t my_mat(numRow);
    mat_t my_mat2 = make_mat(); // or just use our maker
    // Due to that default allocator uses value initialization, a.k.a T().
    // At this point, all positions are value init to int(), which is zero,
    // from value init of array<int, col>() by the default allocator.
    // numVertex x numEdge is one solid contaguous chunk and now ready to roll.

    // range for
    for (row_t r : my_mat) {
        for (int n : r) {
            std::cout << std::setw(4) << n;
        }
        std::cout << '\n';
    }

    // classic for
    for (int i = 0; i < numRow; ++i) {
        for (int j = 0; j < numCol; ++j) {
            std::cout << std::setw(4) << (my_mat2[i][j] = i*numRow + numCol);
        }
        std::cout << '\n';
    }

}

Upvotes: 0

Max Langhof
Max Langhof

Reputation: 23711

Note that matrix = new int**[numVertex]; as mentioned by you would create (for this example) a 3D array, because you'd have numVertex entries of int**.

The (int**) cast does not accomplish much, if anything at all, because if matrix is of type int**, there is no need for the cast (you get back an int** already from the new).

Upvotes: 0

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17444

(int**)value is a C-style cast operation.

Notes:

  • Don't use those in C++, it tends to cause or hide problems, like mismatches between right and left side of an assignment.
  • The code is relatively low quality, proper C++ would rather use std::vector.
  • The code is also not complete, so little can be said with certainty about how it functions.

Upvotes: 6

Related Questions