Changeset 26158 for main/trunk/greenstone2/build-src
- Timestamp:
- 2012-09-07T17:06:09+12:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/build-src/src/phind/generate/suffix.cpp
r26135 r26158 180 180 int suffixCompare(const void *cpa, const void *cpb) { 181 181 182 // Cast then dereference pointers to suffix array elements 183 symbol *pa = (symbol *) cpa; 184 symbol *pb = (symbol *) cpb; 185 pa = (symbol *) *pa; 186 pb = (symbol *) *pb; 182 // The following is a bug and so at times results in a segmentation fault on 64 bit linux machines. 183 // The bug is in the 3rd and 4th lines, which perform identical operations. Considering the 3rd line: 184 // The *pa part of this statement truncates pa, the 64 bit pointer (symbol*), with the 32 bit value 185 // symbol (unsigned int) that it's being dereferenced to, before trying to reconvert it to a pointer 186 // which is always 64 bit on 64 bit machines. The 4th line has the same problem and prefixCompare() too. 187 188 // WRONG: Cast then dereference pointers to suffix array elements 189 //symbol *pa = (symbol *) cpa; 190 //symbol *pb = (symbol *) cpb; 191 //pa = (symbol *) *pa; 192 //pb = (symbol *) *pb; 193 194 // This is the correct way that will work on both 64 bit and 32 bit machines: 195 // the 2 void* arguments to this function, cpa and cpb, are "pointers to elements" as per 196 // http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/. The array elements themselves are also 197 // *pointers* themselves: to symbol values. As the comment to this function says: cpa and cpb are 198 // pointers to pointers, making them double pointers. When we cast the void* cpa and cpb arguments to 199 // the types we need to work with here, they therefore need to be cast as double pointers to symbol. 200 // We declare pa and pb as symbol pointers (symbol*) to the actual values being compared, therefore pa 201 // and pb need to be instantiated as the address of the values referenced by cpa and cpb. 202 symbol *pa = *((symbol **) cpa); 203 symbol *pb = *((symbol **) cpb); 204 187 205 188 206 // If the two elements are the same, examine the next one … … 210 228 int prefixCompare(const void *cpa, const void *cpb) { 211 229 212 // Cast then dereference pointers to prefix array elements 213 symbol *pa = (symbol *) cpa; 214 symbol *pb = (symbol *) cpb; 215 pa = (symbol *) *pa; 216 pb = (symbol *) *pb; 230 // Cast and dereference double pointers (pointers to prefix array elements) 231 // See comments in suffixCompare() 232 symbol *pa = *((symbol **) cpa); 233 symbol *pb = *((symbol **) cpb); 217 234 218 235 // If the two elements are the same, examine the next one … … 222 239 } 223 240 224 // Make the co pmparison and return241 // Make the comparison and return 225 242 if ( *pa > *pb) { 226 243 return -1;
Note:
See TracChangeset
for help on using the changeset viewer.