[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-gsl] gsl_matrix *p Pointer in Object
From: |
Thomas Schmitt |
Subject: |
Re: [Help-gsl] gsl_matrix *p Pointer in Object |
Date: |
Tue, 21 Jul 2009 21:12:25 +0200 |
Ok, maybe it's much shorter if i explain what i want to do:
I wanna have a class with a member variable of the type gsl_matrix.
Every instance of this class should have it's own gsl_matrix.
So what i tried is to make a class member variable with:
private:
gsl_matrix * p_matrix;
and i thought this pointer stays the same with every instance of this
class.
So i tried to initialize the pointer with
gsl_matrix_view a = gsl_matrix_view_array( _array, _rows, _columns );
GaussMatrix::p_matrix = gsl_matrix_calloc(rows,columns);
GaussMatrix::p_matrix = &a.matrix;
you're right, the calloc doesn't make sense!! But the line
GaussMatrix::p_matrix = &a.matrix;
should give me a pointer to the view i create.....but if i try to call
the pointer later in a function of the class(it just prints out the
matrix)
void GaussMatrix::print_matrix(){
for (int i = 0; i < 2; i++) /* OUT OF RANGE ERROR */
for (int j = 0; j < 2; j++)
printf ("m(%d,%d) = %g\n", i, j,
gsl_matrix_get ( p_matrix, i, j));
}
i get different values than the ones i initialized.
I think i missing something in the pointer concept.....
Thanks very very much for your help. Hope i could clarify what i want to
do....
Am Dienstag, den 21.07.2009, 20:48 +0200 schrieb Frank Reininghaus:
> Hi,
>
> On Tuesday 21 July 2009 19:58:22 Thomas Schmitt wrote:
> > My problem is that the
> > pointer changes..... attached ist the code.... Hope someone can help
> > me!!!! Thanks in advance!!!!
>
> I don't really get what the problem is, to be honest. It might help if you
> could
>
> * provide code (as short as possible) that can be compiled and run
> * say what you expect from the code
> * say what actually happens.
>
> I noticed something strange though, maybe it's related to your problem:
>
> > GaussMatrix::p_matrix = gsl_matrix_calloc(rows,columns);
> > GaussMatrix::p_matrix = &a.matrix;
>
> You allocate memory and assign a pointer to it to p_matrix and overwrite that
> pointer in the next line. Does not make any sense to me.
>
> Moreover, it looks like your p_matrix member of the class, which is
> apparently
> supposed to point to the matrix, is static, right? That does not make much
> sense either - it means that every instance of the class has the same matrix
> data, which is probably not what you want.
>
> Cheers
> Frank