I have a class called CEquation for working with polynomials. After a ridiculous amount of math with these, I finally need to arrange them into a matrix, and take the determinant. Here is the problem:
CEquation CMatrixEqn::Det(CEquation**& matrix, int iSize)
Unfortunately, when I do this, I have memory errors with the CEquations.
myprogram in free(): warning: chunk is already free
If I could somehow say:
CEquation CMatrixEqn::Det(const CEquation**& matrix, int iSize)
But this gives me compiler errors. Is this just not possible?
matrixeqn.cpp: In member function `CEquation CMatrixEqn::Determinant()':
matrixeqn.cpp:56: error: no matching function for call to `CMatrixEqn::Det(CEquation**&, int&)'
matrixeqn.h:16: note: candidates are: CEquation CMatrixEqn::Det(const CEquation**&, int)
matrixeqn.cpp: In member function `CEquation CMatrixEqn::Det(const CEquation**&, int)':
matrixeqn.cpp:143: error: no matching function for call to `CMatrixEqn::Det(CEquation**&, int)'
matrixeqn.cpp:69: note: candidates are: CEquation CMatrixEqn::Det(const CEquation**&, int)
I don't think passing consts will keep you from fooling around with free(). You should focus your energy on finding why you're free()ing the same memory location twice.
The memory is freed twice because "matrix" is only shallow copied into the function 'Det'. By making it a constant, the "matrix" is not destroyed when the function returns.
I'd be just as happy to take another way of getting around this (some sort of deep copy). I already have a copy constructor that should be doing a deep copy, so I would prefer to take the easier route of making my CEquation matrix a constant.
Thank you for your reply though! It helped me remind myself where the error was coming from.
Uhh.... What exactly is a "shallow copy". You can either copy data, or pass references to data. I've never heard of "shallow-copying" a matrix.
If Det() doesn't allocate the matrix, it shouldn't deallocate it. Or, if it does deallocate it (this is known as taking onwership), then whoever did allocate it, shouldn't deallocate it.
But, if you really want to copy the matrix, writing a function taking three parameters shouldn't be too hard.
Alright helios, thank you for your help. I ended up recoding the section in such a way that it got around that particular problem.
Since you asked:
A 'shallow copy' is the product of the default copy method and is just the opposite of a 'deep copy'. This means that the two objects have the exact same values for all member variables. But this causes problems with pointers, where now you get two pointers pointing at the same thing. Once one of them is freed, the other is left pointing to an already freed address.
Thanks for your time! I think I'll call this problem solved and move to the next! :-)
Oh, now that I read the Wikipedia article, I understand.
Don't use that term for the simpler data structures such as arrays, since you're not actually copying anything. You're just passing a pointer to the start of the array (okay, you're copying the pointer, but doesn't count). In this case, since this is an array of pointers to arrays, calling it shallow copy might lead to confusion: are you just copying the pointer to the start of the array of pointer to arrays or did you also bit-wise copy the array of pointers and you're passing a pointer the start of that array?
"Shallow copy" is, however, suitable when talking about structures that store pointers to data, not data itself (e.g. std::vector<int *>).
Like I already said, the function shouldn't free the memory in question. If your "recoding" doesn't involve not freeing the matrix, it's likely there are still problems.