I'm working on creating a K-Nearest Neighbor classifier and having some problems that I can't figure out. I have a header file KNNClassifier.h and its class file KNNClassifier.c c
In KNNClassifier.h I have the following declarations:
Everything worked fine until I added classificationM ap and indexMap. After adding those two variables the program seg faults near the end of the KNNClassifier constructor. If I comment out either one of them (it doesn't matter which) the program will complete but Valgrind reports the same type of read/write errors but there is no seg fault. The relevant Valgrind output is below the constructor code snippet.
The seg fault occurs at the last line when trainedFeatures is malloced in Valgrind but somewhere else when Valgrind is not running. Regardless I think the segfault is a symptom of the memory problems with STL.
==11646== 75 errors in context 53 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F07D: std::vector<dou ble, std::allocator< double> >::begin() const (stl_vector.h:3 42)
==11646== by 0x805268D: std::vector<dou ble, std::allocator< double> >::vector(std:: vector<double, std::allocator< double> > const&) (stl_vector.h:2 34)
==11646== by 0x805695B: KNNClassifier:: classifyFeature s(std::vector<d ouble, std::allocator< double> >) (KNNClassifier. cc:436)
==11646== by 0x8057B95: KNNClassifier:: classifyFromFil e(char*, char*, char*) (KNNClassifier. cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB10 is 12 bytes after a block of size 100 alloc'd
==11646== at 0x4021DC5: operator new(unsigned) (vg_replace_mal loc.c:163)
==11646== by 0x805CBE4: main (main.cc:55)
==11646==
==11646== 75 errors in context 54 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F0A9: std::vector<dou ble, std::allocator< double> >::end() const (stl_vector.h:3 60)
==11646== by 0x8052673: std::vector<dou ble, std::allocator< double> >::vector(std:: vector<double, std::allocator< double> > const&) (stl_vector.h:2 34)
==11646== by 0x805695B: KNNClassifier:: classifyFeature s(std::vector<d ouble, std::allocator< double> >) (KNNClassifier. cc:436)
==11646== by 0x8057B95: KNNClassifier:: classifyFromFil e(char*, char*, char*) (KNNClassifier. cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB14 is not stack'd, malloc'd or (recently) free'd
==11646==
==11646== 75 errors in context 55 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F0A9: std::vector<dou ble, std::allocator< double> >::end() const (stl_vector.h:3 60)
==11646== by 0x804F134: std::vector<dou ble, std::allocator< double> >::size() const (stl_vector.h:4 02)
==11646== by 0x805261F: std::vector<dou ble, std::allocator< double> >::vector(std:: vector<double, std::allocator< double> > const&) (stl_vector.h:2 33)
==11646== by 0x805695B: KNNClassifier:: classifyFeature s(std::vector<d ouble, std::allocator< double> >) (KNNClassifier. cc:436)
==11646== by 0x8057B95: KNNClassifier:: classifyFromFil e(char*, char*, char*) (KNNClassifier. cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB14 is not stack'd, malloc'd or (recently) free'd
==11646==
==11646== 75 errors in context 56 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F07D: std::vector<dou ble, std::allocator< double> >::begin() const (stl_vector.h:3 42)
==11646== by 0x804F119: std::vector<dou ble, std::allocator< double> >::size() const (stl_vector.h:4 02)
==11646== by 0x805261F: std::vector<dou ble, std::allocator< double> >::vector(std:: vector<double, std::allocator< double> > const&) (stl_vector.h:2 33)
==11646== by 0x805695B: KNNClassifier:: classifyFeature s(std::vector<d ouble, std::allocator< double> >) (KNNClassifier. cc:436)
==11646== by 0x8057B95: KNNClassifier:: classifyFromFil e(char*, char*, char*) (KNNClassifier. cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB10 is 12 bytes after a block of size 100 alloc'd
==11646== at 0x4021DC5: operator new(unsigned) (vg_replace_mal loc.c:163)
==11646== by 0x805CBE4: main (main.cc:55)
==11646==
==11646== 216 errors in context 57 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F44A: __gnu_cxx::__no rmal_iterator<d ouble*, std::vector<dou ble, std::allocator< double> > >::__normal_ite rator(double* const&) (stl_iterator.h :650)
==11646== by 0x804F4C4: std::vector<dou ble, std::allocator< double> >::begin() (stl_vector.h:3 33)
==11646== by 0x805242D: std::vector<dou ble, std::allocator< double> >::operator[](unsigned) (stl_vector.h:4 80)
==11646== by 0x80550B2: KNNClassifier:: calcFeatureScal e() (KNNClassifier. cc:706)
==11646== by 0x8058479: KNNClassifier:: trainFromFile(c har*, char*) (KNNClassifier. cc:185)
==11646== by 0x805CC93: main (main.cc:65)
==11646== Address 0x567EB10 is 12 bytes after a block of size 100 alloc'd
==11646== at 0x4021DC5: operator new(unsigned) (vg_replace_mal loc.c:163)
==11646== by 0x805CBE4: main (main.cc:55)
In KNNClassifier.h I have the following declarations:
Code:
class KNNClassifier{ ... map<int, string> classNames; map<int, DataStore*> lastDataStores; map<int, int*> classificationMap; map<int, int> indexMap; vector<double> scaleVector; ... };
Code:
KNNClassifier::KNNClassifier(int numNeighbors) { ENTERCONSTRUCTOR; statsInitialized = false; classificationMatrix = NULL; totalClassified = NULL; classifiedCorrectly = NULL; isTrained = false; K = numNeighbors; numVectors = 0; maxVectors = 10; trainedFeatures = (FeatureVector**)malloc(sizeof(FeatureVector*) * maxVectors); LEAVECONSTRUCTOR; }
==11646== 75 errors in context 53 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F07D: std::vector<dou ble, std::allocator< double> >::begin() const (stl_vector.h:3 42)
==11646== by 0x805268D: std::vector<dou ble, std::allocator< double> >::vector(std:: vector<double, std::allocator< double> > const&) (stl_vector.h:2 34)
==11646== by 0x805695B: KNNClassifier:: classifyFeature s(std::vector<d ouble, std::allocator< double> >) (KNNClassifier. cc:436)
==11646== by 0x8057B95: KNNClassifier:: classifyFromFil e(char*, char*, char*) (KNNClassifier. cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB10 is 12 bytes after a block of size 100 alloc'd
==11646== at 0x4021DC5: operator new(unsigned) (vg_replace_mal loc.c:163)
==11646== by 0x805CBE4: main (main.cc:55)
==11646==
==11646== 75 errors in context 54 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F0A9: std::vector<dou ble, std::allocator< double> >::end() const (stl_vector.h:3 60)
==11646== by 0x8052673: std::vector<dou ble, std::allocator< double> >::vector(std:: vector<double, std::allocator< double> > const&) (stl_vector.h:2 34)
==11646== by 0x805695B: KNNClassifier:: classifyFeature s(std::vector<d ouble, std::allocator< double> >) (KNNClassifier. cc:436)
==11646== by 0x8057B95: KNNClassifier:: classifyFromFil e(char*, char*, char*) (KNNClassifier. cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB14 is not stack'd, malloc'd or (recently) free'd
==11646==
==11646== 75 errors in context 55 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F0A9: std::vector<dou ble, std::allocator< double> >::end() const (stl_vector.h:3 60)
==11646== by 0x804F134: std::vector<dou ble, std::allocator< double> >::size() const (stl_vector.h:4 02)
==11646== by 0x805261F: std::vector<dou ble, std::allocator< double> >::vector(std:: vector<double, std::allocator< double> > const&) (stl_vector.h:2 33)
==11646== by 0x805695B: KNNClassifier:: classifyFeature s(std::vector<d ouble, std::allocator< double> >) (KNNClassifier. cc:436)
==11646== by 0x8057B95: KNNClassifier:: classifyFromFil e(char*, char*, char*) (KNNClassifier. cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB14 is not stack'd, malloc'd or (recently) free'd
==11646==
==11646== 75 errors in context 56 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F07D: std::vector<dou ble, std::allocator< double> >::begin() const (stl_vector.h:3 42)
==11646== by 0x804F119: std::vector<dou ble, std::allocator< double> >::size() const (stl_vector.h:4 02)
==11646== by 0x805261F: std::vector<dou ble, std::allocator< double> >::vector(std:: vector<double, std::allocator< double> > const&) (stl_vector.h:2 33)
==11646== by 0x805695B: KNNClassifier:: classifyFeature s(std::vector<d ouble, std::allocator< double> >) (KNNClassifier. cc:436)
==11646== by 0x8057B95: KNNClassifier:: classifyFromFil e(char*, char*, char*) (KNNClassifier. cc:281)
==11646== by 0x805CCD1: main (main.cc:71)
==11646== Address 0x567EB10 is 12 bytes after a block of size 100 alloc'd
==11646== at 0x4021DC5: operator new(unsigned) (vg_replace_mal loc.c:163)
==11646== by 0x805CBE4: main (main.cc:55)
==11646==
==11646== 216 errors in context 57 of 57:
==11646== Invalid read of size 4
==11646== at 0x804F44A: __gnu_cxx::__no rmal_iterator<d ouble*, std::vector<dou ble, std::allocator< double> > >::__normal_ite rator(double* const&) (stl_iterator.h :650)
==11646== by 0x804F4C4: std::vector<dou ble, std::allocator< double> >::begin() (stl_vector.h:3 33)
==11646== by 0x805242D: std::vector<dou ble, std::allocator< double> >::operator[](unsigned) (stl_vector.h:4 80)
==11646== by 0x80550B2: KNNClassifier:: calcFeatureScal e() (KNNClassifier. cc:706)
==11646== by 0x8058479: KNNClassifier:: trainFromFile(c har*, char*) (KNNClassifier. cc:185)
==11646== by 0x805CC93: main (main.cc:65)
==11646== Address 0x567EB10 is 12 bytes after a block of size 100 alloc'd
==11646== at 0x4021DC5: operator new(unsigned) (vg_replace_mal loc.c:163)
==11646== by 0x805CBE4: main (main.cc:55)
Comment