Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return/pointer problem with structures

Status
Not open for further replies.

Biogenicsoup

Programmer
Jul 4, 2005
6
0
0
DK
Hi all

I have tried to generalize my matrix expression in my current program, but have encountered some difficulties with method calls. When i call a method which return void with a pointer to my structure (ex: MatrixInverse(mat, invmat);) some of the elements of mat are erased/changed when i return from the method.

The structure is:
Code:
typedef struct matrix
{ int type;
  bool packed;
  int cols;
  int rows;
  FILE* fp;
  int** MINT; 
  char** MCHAR;
  double** MDOUBLE;
  sparse* MSPARSE;
} matrix;

the place where i call the method looks like this:

Code:
printf("mat->type = %d\n",mat->type);
MatrixInverse(mat, invmat);
printf("mat->type = %d\n",mat->type);

the MatrixInverse method looks like this:
Code:
void MatrixInverse(matrix* inmat, matrix* outmat) 
{ printf("MatrixInverse called\n");
  fflush((FILE*)0);
  int j = 0;
  int i;
  int n = inmat->rows;
  double* col=AllocDoubleArray(n);
  inmat=LUDecomp(inmat);
    
  while (j<n)
    { i=0; 
      while (i<n)
	{ col[i]=0.0;
          ++i; 
	}
      col[j]=1.0;
      LULinEq(inmat, col);
      i=0; 
      while (i<n)
	{ outmat->MDOUBLE[i][j]=col[i];
          ++i;   
	}
      ++j;
    }
  DeallocDoubleArray(col, n);
  
  printf("inmat->type = %d\n",inmat->type);
}

when run the output gives:
mat->type = 2
MatrixInverse called
inmat->type = 2
mat->type = 0

clearly the type variable is changed by returning from the method. This pussles me as i do not change the type variable or the matrix pointer at any time. Does anyone know what I am doing wrong?

Thanks in advance for your help
 
Do you have a linking problem? Try deleting all object files and rebuild.
 
inmat=LUDecomp(inmat);
LULinEq(inmat, col);

perhaps it changes in one of these.

tomcruz.net
 
xwb said:
Do you have a linking problem? Try deleting all object files and rebuild.

I thought so too at first, but deleting the object files makes no difference.

butthead said:
perhaps it changes in one of these.

It should be unchanged. I have added the print statement at the bottom of the MatrixInverse method, At that point inmat->type = 2. The problem is that inmat->type is changed when i return from MatrixInverse.

 
void MatrixInverse(matrix* inmat, matrix* outmat)
{
printf("MatrixInverse called\n");
printf("inmat->type = %d\n",inmat->type);
}


this is werre Id start. then keep adding code until
the error reoccurs.

wish I could be better help

tomcruz.net
 
1. Why
Code:
inmat=LUDecomp(inmat);
? LUDecomp() returns a new pointer?
2. Where is AllocDoubleArray() code? It's very interesting...
3. Don't use all-capitals names for variables. They are for macros in C/C++ (by tradition;)...
 
Code:
 inmat=LUDecomp(inmat);

more than likely losing it here. inmat is already
assigned an address, and LUDecomp may be assigning a completely different address, and wiping out the old one.

LUDecomp could release and free inmat coming in, and allocate a new address, but you would have to return the new address to your calling routine.
 
cdlvj

if

inmat=LUDecomp(inmat);

did as you supect and trashed the previous address we
would be accessing an improper memory space. as this has
been freed perhaps by LUDecomp(); the memory is not
overwritten just deallocated. would not the space still
contain the old value.

just on a lark Biogenicsoup would you try to access some of
the other members of the matrix like you do on the last call
of your printf and see what you get.


probably dont know what Im talking about though so
nevermind.

:)

tomcruz.net

ps
please post LUDecomp(); as all of us are curious
 
did as you supect and trashed the previous address we
would be accessing an improper memory space. as this has
been freed perhaps by LUDecomp(); the memory is not
overwritten just deallocated. would not the space still
contain the old value

As per the ptr containing the old value, all depends on the compiler, most of the time it would. But I have seen apps running for years, and all of a sudden bam.

If LUDecomp starts with address 0x001000 for inmat, does a free, then mallocs and gets memory, inmat will get a new address say 0x005000. But when the function MatrixInverse returns, inmat will be set back to 0x001000 which would an invalid space.

Right before the call put a printf of the variable inmat, and right afterward. I believe %h gives you the hex, or it may be %x.
 
If you're not particularly fundamentally interested in tracking down what's gone wrong, but just need working LUdecomposition code for a larger problem, had you considered using code from something like "Numerical methods in C" (Sedgewick et al., I think), who have pretty generous terms about use of their code in non-commercial applications, if that's what you are...???

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top