I have a simple program that should sort an array of structs.
I'm using two compilers Visual Studio and Dev-C++.
in Visual Studio the program runs and works fine but in Dev-C++ compile It shows an exception "An Access Violation (Segmentation Fault) raised in your program" and it is not helping. I traced application and found the exception comes from the line that it tries to swap the nodes.
constint MaxAsciiChar = 256;
constint HuffmanTableSize = MaxAsciiChar + MaxAsciiChar -1;
typedefstruct huffmanCode {
// charachter code or leaf code
int CharCode;
// Frequency of charachter appearence
longint Freq;
// Just to show freq in tree view
longint NodeFreqView;
//links to
int parentLink;
int LeftLink;
int RightLink;
// final huffman code will be here
string Huffman;
};
huffmanCode FreqTable[HuffmanTableSize];
void QuickSort(huffmanCode* Array, int Lo, int Hi)
{
// Check for non-base case
if (Lo < Hi){
// Pivot with first element
int iLo = Lo + 1;
huffmanCode Median = Array[Lo];
int iHi = Hi;
huffmanCode Temp;
// Partition array elements
while (iLo <= iHi) {
// Find item out of place
while ((iLo <= iHi) && (Array[iLo].NodeFreqView<=Median.NodeFreqView))
iLo++;
while ((iLo <= iHi) && (Array[iHi].NodeFreqView>Median.NodeFreqView))
iHi--;
// Swap values if necessary
if (iLo < iHi) {
Temp = Array[iLo];
Array[iLo] = Array[iHi];
Array[iHi] = Temp;
iLo = iLo + 1;
iHi = iHi - 1;
}
}
// Move pivot element
Temp = Array[Lo];
Array[Lo] = Array[iHi];
Array[iHi] = Temp;
// Split and sort partitions
QuickSort(Array, Lo, iHi - 1);
QuickSort(Array, iHi + 1, Hi);
}
}
int main(){
...
QuickSort(FreqTable,0,HuffmanTableSize-1);
...
}