N
N
newmersedez2021-03-16 20:50:50
OOP
newmersedez, 2021-03-16 20:50:50

Why does it throw a segmentation fault when the constructor is called?

Hello, there was such a problem. There is a default constructor Square_matrix() that creates a default 1x1 matrix. There is a constructor Square_matrix(unsigned int size) in which I create a matrix of size x size. So, if size == 1 is passed, I call the default constructor. However, this throws a seg fault and I can't figure out why.

main.cpp:

#include <iostream>
#include "square_matrix.h"
using namespace std;

int main(void)
{
    Square_matrix   matrix1;
    Square_matrix   matrix2(1);

    cout << "matrix1: " << endl;
    matrix1.fill_matrix();
    cout << "filled matrix1" << endl;
    matrix1.print_matrix();
    cout << "printed matrix1" << endl;
    
    cout << "\nmatrix2: " << endl;
    matrix2.fill_matrix();
    cout << "filled matrix2" << endl;
    matrix2.print_matrix();
    cout << "printed matrix2" << endl;
    
    return (0);
}


square_matrix.cpp: (fragment with constructors)
#include <iostream>
#include <time.h>
#include "square_matrix.h"

Square_matrix::Square_matrix()
{
    this->size = 1;
    try
    {
        matrix = new double *[size];
        matrix[0] = new double[size];
    }
    catch(const std::bad_alloc& ex)
    {
        std::cerr << ex.what() << '\n';
    }
}

Square_matrix::Square_matrix(unsigned int size)
{
    if (size < 1)
        std::cerr << "Invalid matrix size" << '\n';
    else if (size == 1)
    {
        this->size = 1;
        Square_matrix();
    }
    else
    {
        this->size = size;
        try
        {
            matrix = new double *[size];
            for (unsigned int i = 0; i < size; i++)
                matrix[i] = new double [size];
        }
        catch(const std::bad_alloc& ex)
        {
            std::cerr << ex.what() << '\n';
        }
    }
}

void        Square_matrix::fill_matrix()
{
    srand(time(NULL));
    for(int i = 0; i < size; i++)
        for(int j = 0; j < size; j++)
            matrix[i][j] = rand() % 10;
}

void        Square_matrix::print_matrix()
{
    for(unsigned int i = 0; i < size; i++)
    {
        for(unsigned int j = 0; j < size; j++)
        {
            std::cout << matrix[i][j] << " ";
        }
    std::cout << '\n';
    }
}


PS: I know that there are no checks in the fill_matrix and print_matrix functions, I wrote them to test the creation of a matrix and its filling, the problem is not in them.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2021-03-16
@newmersedez

Square_matrix::Square_matrix(unsigned int size)
{
    if (size < 1)
        std::cerr << "Invalid matrix size" << '\n';
    else if (size == 1)
    {
        this->size = 1;
        Square_matrix();
    }

if size == 1 is passed, I call the default constructor

Square_matrix();-- is not a call to the default constructor for the current object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question