Hi everyone, working on a data structers project on dev-c++ as compiler and problem is when compile&run it freezes but when click debug, (magically) it works. So what do you think I missing
Hi everyone, working on a data structers project on dev-c++ as compiler and problem is when compile&run it freezes but when click debug, (magically) it works. So what do you think I missing
Your code is probably doing an invalid operation with a pointer. Anything that reorganises how your program uses memory can affect what happens as a result of an invalid pointer operation (including symptoms seeming to disappear when debugging, but coming back otherwise).
my struct is like that:
So can't see the Error in this function.Code:struct charDesc { char ch; int freq; int index; }; struct charDesc chars[256]; struct HeapStruct { int capacity; int size; struct charDesc *elements; }; typedef struct HeapStruct *priorityQueue;
Code:void InsertPriorityQueue(int index,int freq,char ch, PriorityQueue h) { int i; if (IsFullPriorityQueue(h)) { printf("Priority queue is full!\n"); return; } h->size++; for (i = h->size; h->elements[i/2].freq > freq; i /= 2) { h->elements[i].ch = h->elements[i/2].ch; h->elements[i].freq = h->elements[i/2].freq; h->elements[i].index = h->elements[i/2].index; } h->elements[i].index = index; h->elements[i].freq = freq; h->elements[i].ch = ch; }
Last edited by zebani; 12-28-2011 at 11:57 AM.
any opinion?
You are starting your for loop at i = h->size, which may be out of range (depending on the rest of your code). You may want i = h->size - 1 instead.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Thank you for all guys. The thing is code working now without changes. I guess dynamically allocated memory caused this crash so when debugging there was no error. And thanks for hint iMalc, I wasn't sure this way is OK