Colin Evrard
Colin Evrard

Reputation: 17

error using 2D pointers

i'm new to this site, so please excuse me if i ask my question in a wrong way.

For my programmation course, i've been asked to code a function which will give me the product of two square matrices (3x3) using pointers.

Here is the code i wrote:

    //This function make a LxC matrix out of a double pointer
void matrixMake(double **a,int unsigned l,int unsigned c)
{
    a=new double*[l];
    for(int i=0;i<l;i++)
    {
        a[i]=new double[c];
    }
}
//This function returns a random number between a and b
double randU(double a=0.0,double b=10.0)
{
    double x=(double)(rand()/RAND_MAX);
    return(a*x+b*(1-x));
}

//This is the function that seems to be a problem, this function creates a matrix and fills it with random numbers between 0 and 10.
double ** matrixGen()
{
    double **matrix;
    matrixMake(matrix,3,3);
    for(int i=0;i<3;i++)
    {
        for(int n=0;n<3;n++)
        {
            matrix[i][n]=randU();
        }
    }
    return(matrix);
}

It compiles fine except when i run the program, it gives me an ugly segmentation error. I tried debugging it, it crashes when it runs the line matrix[i][n]=randU(); is executed.

It didn't give you the full code, the rest is irrelevant to my question and seems to work fine.

I hope my question is not too dumb ^^... Thanks in advance! :D

Upvotes: 1

Views: 62

Answers (3)

ForeverStudent
ForeverStudent

Reputation: 2537

The problem happens because when you pass in double **matrix to your matrixMake function, the memory address is copied, so anything you do to a inside that function will not stick to double **matrix in your main function.

you could try this:

double ** matrixMake(int unsigned l,int unsigned c)
{
    double ** a;
    a=new double*[l];
    for(int i=0;i<l;i++)
    {
        a[i]=new double[c];
    }

    return a;
}

and inside your main function have

double **matrix = matrixMake(3,3);

instead of

double **matrix;
matrixMake(matrix,3,3);

you can also try to pass in double** to your matrixMake either by pointer or by reference, but that would make you a 3 star programmer, which is not a good thing. it would look like this anyway:

void matrixMake(double ***a,int unsigned l,int unsigned c)
{
    *a=new double*[l];
    for(int i=0;i<l;i++)
    {
        *a[i]=new double[c];
    }
}

and instead of matrixMake(matrix,3,3); in your code you would have matrixMake(&matrix,3,3);

Upvotes: 1

A.S.H
A.S.H

Reputation: 29332

In addition to correcting the signature of matrixMake:

void matrixMake(double**& a, unsigned l, unsigned c)

You should also correct this:

double x=(double)(rand()/RAND_MAX); // x always evaluates to zero

into this:

double x=((double)rand())/RAND_MAX);

Upvotes: 2

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

void matrixMake(double **a,int unsigned l,int unsigned c)
{
    a=new double*[l];
    for(int i=0;i<l;i++)
    {
        a[i]=new double[c];
    }
}

This function takes a pointer in by value, and then immediately assigns to it, ignoring the value that was passed in. If you want to modify the pointer, you need to either pass it in by reference (double**&), or by pointer (double***). Or you could just return a fresh pointer from the function, since its value doesn't depend upon what was passed in.

double** matrixMake(int unsigned l,int unsigned c)
{
    double** a=new double*[l];
    for(int i=0;i<l;i++)
    {
        a[i]=new double[c];
    }
    return a;
}

Upvotes: 0

Related Questions