| mothergoose729 (29) | |||||||||
|
I've looked over my code top and bottom, and I am at a loss. I really have no idea what is causing my code to crash. I am beginning to think that a part of my code I think is working is messing with memory in some other place... I really don't know. This is the class I am working on: polynomial.h
poynomial.cpp
main.cpp
The part of the program causing the crash is this member function. If I comment this code out the program seems to work just fine.
If I comment out a line in the destructor that deletes fx*, the program will not crash. I have no idea why that is. Something is happening when I create a new polynomial and have my polynomial pointer temp2 try and access it and return it, but I don't know how or why its causing a segmentation fault. The output when this program is run is exactly what I am expecting. The function works but someplace there is a memory issue. The purpose of this program is to return the 1st, 2nd, 3rd, 4th or whatever derivative of the polynomial that was called. Each polynomial has a pointer as a member that points to another polynomial which is its derivative. I really need some help on this one. Stared at it for a couple hours but can't figure out what is going on. | |||||||||
|
|
|||||||||
| clanmjc (661) | |
| It looks like in some cases fx is allocated using [] and in other places (). If this is the case then delete [] with most definitely crash. | |
|
|
|
| cire (1851) | |||
|
That destructor looks very wrong. fx is allocated, depending on which constructor is used, by either new[] or new. The delete in the destructor must match the call to new. Line 20 in polynomial.cpp allocates space for one double and initializes it to f.polyDegree+1. I don't think that's what you meant to do since you then go on to treat it as if it's an array of size polyDegree+1. You have a memory leak in polynomial::fPrime(). You allocate memory via new and then throw away the address. The destructor, after a short review of the code, looks like it should be (assuming you fix the one constructor):
You don't actually allocate any memory for critPnts or realRoots, but it looks like you may in the future, and they will clearly be array type allocations. And, since it is perfectly okay to call delete on null pointers, there's no reason not to include them in the destructor. However, there should be no loop in the destructor. The polynomial you new'd for derivative is responsible for deleting it's own members. If you recursively delete them in the destructor you're going to be deleting the same pointer values multiple times and that's a recipe for undefined behavior. | |||
|
Last edited on
|
|||
| mothergoose729 (29) | |
|
Thank you guys so much! Such a simple fix and such a big problem. If I could trouble you a little more, what is the difference between pointer = new type[]; as compared to pointer = new type(); cire I appreciate your critique of my method. Given that, for the goals I have for the class, it would make sense to keep a polynomials derivative in memory, how would you suggest solving this problem? Would it be most proper to call derivative every time I need a derivative and then deallocate in the same step? Seems costly in terms of performance. Thanks again. That was driving me nuts. | |
|
|
|
| firedraco (5414) | |
|
The first is invalid. You need to have a number between the [ ]. If that is the case, then: pointer = new type[n]; will create an array of n types and return a pointer to the first element.pointer = new type(); will create a single type and return a pointer to it.
| |
|
|
|
| cire (1851) | |||||
Well, the first allocates memory for an array of type variables and constructs them in-place via the default constructor. The second allocates memory for one type variable and constructs it in that memory via the default constructor. Any time you use new type(args), the compiler will try to forward those arguments to a type constructor after the memory is allocated to construct the object.
There's nothing wrong with what you've done. If you're referring to the fPrime function you don't need to use new there at all.
would work fine. | |||||
|
|
|||||
| ne555 (4041) | |||
| |||
|
Last edited on
|
|||