Segmentation fault

Hey,
I am trying to write this hierarchical program: An area (rectangle) is defined by two Sides, which again is defined by two Points. Upon devision, the Area is subdivided with new points.... During the second hierarchical subdivision the program segfaults. The first works fine.
I guess it is connected to the way I dynamically allocate memory, copy the content of the old array into the new one and delete the previous one afterwards and return the new one (all done in Area::divide). But I cannot figure out how that causes a segmentation fault.

Since the files (include and main) is too large for 8192 char:
http://www.sendspace.com/file/6nzbwe
it is 3kB.

Can somebody help? Much thanks.

I need to have all points be accessible in one array, for the final program. Thats why I did not make a real hierarchical model.
Steffen
It's too late and I can't think clearly enough to find segfaults right now, so I'll only give you some of the problems with your code:

There are quite a few abstraction problems with your code. The most obvious is that you're setting everything manually. Lines 201, 202, 215, 216, 219, 222, 223, and 225 of main5.cpp. None of those belong in main(). Your classes are mess and their innards are spilled all over your program. My guess is that somewhere you made a mistake, possibly set something to the wrong value, and that made some code access memory it shouldn't have or a destructor freed memory it shouldn't have.

What's the point of calling the unset() functions in the destructor of HierModel?

On line 230 of main5.cpp, i starts at 1, so the for never runs.

Something tells me Area::divide() has much more responsibility than it should.

Line 112 of main5.cpp doesn't take into account for RAND_MAX<1000000.

If you can provide the line where the segmentation fault happens, it would help.
Hey,
thanks for the hints, but could you be more specific:
What are the abstraction problems with the code?
And how to be less messy?

Now to the answers:
Yes, I set everything manually in those lines, I could transfer those lines into the class, but that would not change the seg-fault and I thought it is clearly labeled as initialization. And it works, well that is not a good excuse.

Unset() to set all the pointers to the Null-pointer, such that the deletion can proceed without any values being deleted. Does that make sense?

line 230: yes, area[0] is already divided, hence it does not need to be divided again. Since numArea is by now 4 or 20, the for-loop should run.

Yeah, I agree, but I have not figured out a way to reduce it. I guess that's why the seg-fault happens.

randMax: True, should be fixed by a simple if statement. it is non-critical at this moment.

line it seg-faults: Backtrace:
#4 main(): at main5.cpp:231
#3 Area::divide() at main5.cpp:182
#2 Point::calcXY() at main5.cpp:44
#1 Side::endgetX() at myinclude.h:72
#0 Point::getX() at myinclude.h:49

Again, thanks for your hints and I am looking forward to your clarification.
Steffen
What are the abstraction problems with the code?
"Abstraction" wasn't the right word. "Encapsulation" was.

Yes, I set everything manually in those lines, I could transfer those lines into the class, but that would not change the seg-fault and I thought it is clearly labeled as initialization. And it works
Evidently it doesn't work, because you're segfaulting.

Unset() to set all the pointers to the Null-pointer, such that the deletion can proceed without any values being deleted. Does that make sense?
No. It's pointless to set members just before the object is deleted

randMax: True, should be fixed by a simple if statement. it is non-critical at this moment.
It's not non-critical if your code expects to receive at some point 1000000. I don't know if that's the case.

I wouldn't keep touching this code before properly encapsulating it
Hmm, I thought I used encapsulation. Apparently not.

I will write a simpler program that uses the same "algorithm", and which encapsulates more. If it seg-faults again, I repost it as a new topic.

Thanks for the help, Helios.
Topic archived. No new replies allowed.