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
Printable View
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;
}
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.
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