title: Segmentation fault -- How!
body: <pre><code>int Sequence::scoreDegeneracy()
  {
      cout &lt;&lt; "Score Degeneracy" &lt;&lt; name &lt;&lt; seqLen &lt;&lt; endl;
      int f = 0;
      if (degenComputed == false)
      {
          char _2foldTest = '*';
          char _4foldTest = '*'; 
          int aaseqLen = seqLen/3;
          int i = 0;
          for (i; i &lt; aaseqLen; i++)
  
          {
              _4foldTest = is4FoldDegenerateSite(i);
              _2foldTest = is2FoldDegenerateSite(i,_4foldTest);
              degScores.totalCodons++
                  if (_2foldTest != '*')
              {
                  degScores.totalCodons_2fold++;
                  if (_2foldTest == 'A')
                  {
                      degScores.total_2fold_A++;
                  }
                              else if (_2foldTest == 'T')
                  {
                      degScores.total_2fold_T++;
                  }
                              else if (_2foldTest == 'G')
                  {
                      degScores.total_2fold_G++;
                  }
                               else
                  {
                      degScores.total_2fold_C++;   
                  }  
              }else if (_4foldTest != '*')
              {
                  degScores.totalCodons_4fold++;
                  if (_4foldTest == 'A')
                  {
                      degScores.total_4fold_A++;
                  }
                              else if (_4foldTest == 'T')
                  {
                      degScores.total_4fold_T++;
                  }
                              else if (_4foldTest == 'C')
                  {
                      degScores.total_4fold_C++;
                  }
                              else
                  {
                      degScores.total_4fold_G++;
                  }
              }
              cout &lt;&lt; "Crashes right here when i = 0." &lt;&lt; endl;        
          } 
      }
      degenComputed = true;
      return 1;
  }
  </code></pre>
  
  <p>Driving me nuts! SegFault happens right at end of first iteration of for loop. Behaves EXACTLY like it supposed to, first test returns right results.. The segmentation fault happens before i is even incremented....</p>
  
  <p>What am I missing??</p>
  
  <p>...</p>
  
  <pre><code>char Sequence::is4FoldDegenerateSite(int codonIndex){
   char aminoAcid = aaSeq[codonIndex];
   //cout &lt;&lt; "Amino acid of codon number " &lt;&lt; codonIndex &lt;&lt; " is: " &lt;&lt; aminoAcid &lt;&lt; endl;
   int loc = readingFrame + (codonIndex * 3) + 2;  
   int locR = loc - 2;
   switch (aminoAcid){
    case 'A':
     return seq[loc];
    case 'T':
     return seq[loc];
    case 'V':  
     return seq[loc];      
    case 'G':
     return seq[loc];
    case 'P':
     return seq[loc];
    case 'S'://Can also be 2 fold degenerate
     if (seq[locR] == 'T'){ 
      return seq[loc];
     }else{ 
      return '*';
     }    
    case 'R'://Can also be 2 fold degenerate
     if (seq[locR] == 'C'){
      return seq[loc];
     }else{ 
      return '*';
     }
    case 'L'://Can also be 2 fold degenerate
     if (seq[locR] == 'C'){ 
      return seq[loc];
     }else{ 
      return '*';   
     }   
   }  
   return '*';
  }
  char Sequence::is2FoldDegenerateSite(int codonIndex, char _4FoldResults){
   char aminoAcid = aaSeq[codonIndex];
   int loc = readingFrame + (codonIndex * 3) + 2;
   //char doubleDegeneracyTest = is4FoldDegenerateSite(codonIndex);
   //bool doubleDegeneracy = false;
   //if (doubleDegeneracyTest != '*') doubleDegeneracy = true;  
   switch (aminoAcid){
    case 'N':
     return seq[loc];
    case 'D':
     return seq[loc];
    case 'C':  
     return seq[loc];      
    case 'Q':
     return seq[loc];
    case 'E':
     return seq[loc];
    case 'H':
     return seq[loc];
    case 'K':
     return seq[loc];
    case 'F':
     return seq[loc];
    case 'Y':
     return seq[loc];
    case 'X':
     return seq[loc];
    case 'S'://Can also be 2 fold degenerate
     if (_4FoldResults == '*'){ 
      return seq[loc];
     }else{ 
      return '*';
     }    
    case 'R'://Can also be 2 fold degenerate
     if (_4FoldResults == '*'){
      return seq[loc];
     }else{ 
      return '*';
     }
    case 'L'://Can also be 2 fold degenerate
     if (_4FoldResults == '*'){ 
      return seq[loc];
     }else{ 
      return '*';   
     }   
   }  
   return '*';
  }
  </code></pre>
  
  <p>Valgrind Results:</p>
  
  <pre><code>**17043** new/new[] failed and should throw an exception, but Valgrind
  ==17043==    at 0x4024E35: VALGRIND_PRINTF_BACKTRACE (valgrind.h:3720)
  ==17043==    by 0x40253C2: operator new(unsigned int) (vg_replace_malloc.c:214)
  ==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator&lt;char&gt; const&amp;, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D83AF: std::string::append(std::string const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x804BA7F: std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; std::operator+&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) (basic_string.h:2168)
  ==17043==    by 0x804ACDF: GenomeSeq::setup() (GenomeSeq.C:108)
  ==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
  ==17043==    by 0x80496DF: main (Main2.C:143)
  **17043**    cannot throw exceptions and so is aborting instead.  Sorry.
  ==17043==    at 0x4024E35: VALGRIND_PRINTF_BACKTRACE (valgrind.h:3720)
  ==17043==    by 0x40253D0: operator new(unsigned int) (vg_replace_malloc.c:214)
  ==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator&lt;char&gt; const&amp;, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D83AF: std::string::append(std::string const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x804BA7F: std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; std::operator+&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) (basic_string.h:2168)
  ==17043==    by 0x804ACDF: GenomeSeq::setup() (GenomeSeq.C:108)
  ==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
  ==17043==    by 0x80496DF: main (Main2.C:143)
  Loading sequence..==17043== 
  ==17043== HEAP SUMMARY:
  ==17043==     in use at exit: 2,749,679 bytes in 10 blocks
  ==17043==   total heap usage: 39,043 allocs, 39,032 frees, 80,022,672,644 bytes allocated
  ==17043== 
  ==17043== 27 bytes in 1 blocks are definitely lost in loss record 1 of 10
  ==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
  ==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7340: ??? (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7525: std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x804969E: main (Main2.C:143)
  ==17043== 
  ==17043== 28 bytes in 1 blocks are definitely lost in loss record 2 of 10
  ==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
  ==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7340: ??? (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7525: std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x80496C3: main (Main2.C:143)
  ==17043== 
  ==17043== 83 bytes in 1 blocks are definitely lost in loss record 3 of 10
  ==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
  ==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator&lt;char&gt; const&amp;, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D814A: std::string::append(char const*, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40B3F3E: std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::getline&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, char) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40C9799: std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::getline&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x804ACB5: GenomeSeq::setup() (GenomeSeq.C:106)
  ==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
  ==17043==    by 0x80496DF: main (Main2.C:143)
  ==17043== 
  ==17043== 100 bytes in 1 blocks are definitely lost in loss record 4 of 10
  ==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
  ==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator&lt;char&gt; const&amp;, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D814A: std::string::append(char const*, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40B3F3E: std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::getline&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, char) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40C9799: std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::getline&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x804AC07: GenomeSeq::setup() (GenomeSeq.C:101)
  ==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
  ==17043==    by 0x80496DF: main (Main2.C:143)
  ==17043== 
  ==17043== 100 bytes in 1 blocks are definitely lost in loss record 5 of 10
  ==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
  ==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7340: ??? (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D75D1: std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D33C5: std::basic_istringstream&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_istringstream(std::string const&amp;, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x804AC42: GenomeSeq::setup() (GenomeSeq.C:103)
  ==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
  ==17043==    by 0x80496DF: main (Main2.C:143)
  ==17043== 
  ==17043== 8,192 bytes in 1 blocks are definitely lost in loss record 8 of 10
  ==17043==    at 0x4025024: operator new[](unsigned int) (vg_replace_malloc.c:258)
  ==17043==    by 0x40A2592: std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::_M_allocate_internal_buffer() (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40A65B1: std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::open(char const*, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40A7EAF: std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;::basic_ifstream(char const*, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x804ABBF: GenomeSeq::setup() (GenomeSeq.C:97)
  ==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
  ==17043==    by 0x80496DF: main (Main2.C:143)
  ==17043== 
  ==17043== 8,192 bytes in 1 blocks are definitely lost in loss record 9 of 10
  ==17043==    at 0x4025024: operator new[](unsigned int) (vg_replace_malloc.c:258)
  ==17043==    by 0x40A2592: std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::_M_allocate_internal_buffer() (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40A65B1: std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::open(char const*, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40A7EAF: std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;::basic_ifstream(char const*, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x804AC9D: GenomeSeq::setup() (GenomeSeq.C:105)
  ==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
  ==17043==    by 0x80496DF: main (Main2.C:143)
  ==17043== 
  ==17043== 2,732,253 bytes in 1 blocks are definitely lost in loss record 10 of 10
  ==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
  ==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator&lt;char&gt; const&amp;, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x40D83AF: std::string::append(std::string const&amp;) (in /usr/lib/libstdc++.so.6.0.13)
  ==17043==    by 0x804BA7F: std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; std::operator+&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) (basic_string.h:2168)
  ==17043==    by 0x804ACDF: GenomeSeq::setup() (GenomeSeq.C:108)
  ==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
  ==17043==    by 0x80496DF: main (Main2.C:143)
  ==17043== 
  ==17043== LEAK SUMMARY:
  ==17043==    definitely lost: 2,748,975 bytes in 8 blocks
  ==17043==    indirectly lost: 0 bytes in 0 blocks
  ==17043==      possibly lost: 0 bytes in 0 blocks
  ==17043==    still reachable: 704 bytes in 2 blocks
  ==17043==         suppressed: 0 bytes in 0 blocks
  ==17043== Reachable blocks (those to which a pointer was found) are not shown.
  ==17043== To see them, rerun with: --leak-check=full --show-reachable=yes
  ==17043== 
  ==17043== For counts of detected and suppressed errors, rerun with: -v
  ==17043== ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 19 from 8)
  </code></pre>
  
  <p>Sequence.h:</p>
  
  <pre><code>#ifndef SEQUENCE_H
  #define SEQUENCE_H
  using namespace std;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cstring&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  class GeneSeqs;
  class Sequence{
      public:
          Sequence();
          Sequence(char * seqStart,char * aaSeqStart, int length);//Constructor
          ~Sequence();//Destructor -- for the transprof
          //void setup();//Setups up variables.
          char &amp;operator[](int);
          char * getSeq(){return seq;}//returns the address of sequence
          string getAASeq(){return aaSeq;}//returns address of aaSeq
          int getLength(){return seqLen;}
          bool isPositive(){return positiveStrand;}
          /**
           * Setters
           */ 
          void setRevC(char * sequ){revC = sequ;}
          void setSeq(char * sequ){
              seq = sequ;
          }
          void setAASeq(char * a){aaSeq = a;}
          void setLength(int len){seqLen = len;}
          void setAALength(int len){aaSeqLen = len;}
          void setPositivity(bool trufal){
              positiveStrand = trufal;
              if (trufal == false){
                  buildRevC();
              }
          }
          //
          int getTCount(){return count.T;}
          int getACount(){return count.A;}
          int getCCount(){return count.C;}
          int getGCount(){return count.G;}
          //void setAASeq(int * a){aaSeq = a;}
          string get_aaSeq(){return aaSeq;}
          bool get_aaSeqExhists(){return aaSeqTranslated;}
          int get_aaSeqLen(){return aaSeqLen;}
          void print();
          void printAA();
          char getAA(int i){return aaSeq[i];}
          int getAALoc(char aa){return AAchar2num(aa);}
          float getMIMScore(){return MIMScore;}
          void setMIM(float MIM){MIMScore = MIM;}
          void translateToAA(int code);
          void computeCrudeCompositions();
          int scoreDegeneracy();
          void degeneracyReport();
          void setName(string n){name = n;}
          float distanceMeasure(Sequence * target);
          bool degeneracyIsScored(){return degenComputed;}
          float distanceMeasure(GeneSeqs * target);
          float distanceMeasureA(GeneSeqs * target, int point, float runningSum);
          int getDegenData(int code);
          void setStartConstant(int i){start = i;}
          void printInfo();
      private:
          struct nucleotideCounts{
              int T;
              int C;
              int A;
              int G;      
          }count;
          struct degeneracyScores{
              int totalCodons;
              int totalCodons_4fold;
              int totalCodons_2fold;
              int total_4fold_A;
              int total_4fold_T;
              int total_4fold_C;
              int total_4fold_G;
              int total_2fold_A;
              int total_2fold_T;
              int total_2fold_G;
              int total_2fold_C;          
          }degScores;
          string name;
          char is4FoldDegenerateSite(int codonIndex);
          char is2FoldDegenerateSite(int codonIndex, char _4FoldResults);
          bool degenComputed;
          float MIMScore;
          void buildRevC();
          string revC;
          char nucleoRevC(char c);
          char * seq;
          int readingFrame;//zero till used...
          int aaSeqLen;
          bool aaSeqTranslated;
          int start;
          int seqLen; //Number of nucleotides in sequence
          bool positiveStrand; //Positive strand = true, negative = false;
          //bool hasY;
          //Genetic sequence in numerical Amino Acide representation;
          string aaSeq;
          void geneSegments2AASeq();
          void computeAATransProf();
          int AAchar2num(char x);
          char AAnum2char(int x);
          char codon2AA(int codonIndex);
          char codon2AA(char * codon);
          char codon2AA(int * codon);
          int Int(string num);    
          int Int(char num);
          char num2nucleo(int a);
          void codonSeq2AASeq();
          int char2nucleo2(char a);
          string String(int num);     
  };
  #endif
  </code></pre>