Problem with string in struct

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.

here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const int MaxAsciiChar = 256;
const int HuffmanTableSize = MaxAsciiChar + MaxAsciiChar -1;

typedef struct huffmanCode {
  // charachter code or leaf code
  int CharCode;
  // Frequency of charachter appearence
  long int Freq;
  // Just to show freq in tree view
  long int 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);
...
}

can anybody help with that?

Last edited on
Topic archived. No new replies allowed.