View Single Post
Werg22
Werg22 is offline
#12
Dec2-08, 12:51 PM
P: 1,520
Quote Quote by D H View Post
Rather than asking us to debug your program for you, it is a good idea for you to learn how to do it yourself. Some guidelines:

(1) Learn how to use your debugger. I cannot underestimate the importance of doing this. Many professional organizations require that programmers verify their code by using the debugger to step through each and every line of code.

(2) Really learn how to use your debugger. Put break points in appropriate places and add conditions to your breakpoints so you can make the code zip over the parts you know are working correctly.

(3) Learn to love your debugger. It is one of your best allies in producing correct code.

(4) Add some print statements. Sometimes the ultimate cause of an error and the manifestation of the error are so far apart that identifying the cause with the debugger can be difficult.
Thanks for the pointers (no pun intended).

Quote Quote by Coin View Post
A good thing to try in this case would be to printf("Pointer %x\n", pointer); every time you allocate or free a term structure. This will allow you to quickly see (1) is everything you free() something you did in fact malloc()? (2) Do you ever free() anything more than once? It isn't Purify, but it may be enough to find the problem...

(I am still very confused why you need free() statements at all. This is supposed to be a sorting mechanism of some kind? Why on earth would any sort need to delete elements from the list as it goes?)
It's because the linked structures actually represent polynomials. What I'm trying to do is relink those structures so that:

1. The coefficients are reduced modulo p
2. There are only terms with non-zero coefficients
3. No two terms have the same power (no repetition of power)

So when I, for example, see that the coefficient of a term reduces to 0 modulo p, I might as well just free the memory that it occupies because I don't need it. Thanks for the suggestion though, I'll try putting printf() statements to see what I get.

Quote Quote by adriank View Post
Another thing that might be happening: If you allocate an array of things all at once using malloc() or similar, then you must free() the entire array at once. You can't free() individual elements of the array--if you try to free the first one, you'll end up freeing the entire array; attempting to free any other element will just segfault, since you didn't allocate them individually.
I see. But what I've done is this:

struct term *p = malloc(n*sizeof(struct term)).

Doing something like

free(p + 1);

doesn't change that I can still access p, p + 2, ... , p + n - 1, right?