Strange Problem of double free corruption

This is a discussion on Strange Problem of double free corruption within the C++ Programming forums, part of the General Programming Boards category; Hi, I have encountered a strange problem of double free corruption. Through debugging, I find the problem is at the ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Strange Problem of double free corruption

    Hi, I have encountered a strange problem of double free corruption. Through debugging, I find the problem is at the end of the entropy function, but I cannot figure out why. Thanks for the help.

    Code:
    #include<iostream>
    #include<iomanip>
    #include<string.h>
    #include<fstream>
    #include<sstream>
    #include<vector>
    #include<string>
    #include<math.h>
    #include<stdlib.h>
    #include<map>
    using namespace std;
    
    int p1_to_num_MJ(string aa){
        map <string,int> AAcode;
         AAcode["A"] = 8;
         AAcode["C"] = 0;
         AAcode["D"] = 14;
      AAcode["E"] = 15;
      AAcode["F"] = 2;
      AAcode["G"] = 9;
      AAcode["H"] = 16;
      AAcode["I"] = 3;
      AAcode["K"] = 18;
      AAcode["L"] = 4;
      AAcode["M"] = 1;
      AAcode["N"] = 12;
      AAcode["P"] = 19;
      AAcode["Q"] = 13;
      AAcode["R"] = 17;
      AAcode["S"] = 11;
      AAcode["T"] = 10;
      AAcode["V"] = 5;
      AAcode["W"] = 6;
      AAcode["Y"] = 7;
      AAcode["X"] = -1;//STOP
    
      //check for valid amino acid
      map <string, int> :: const_iterator Iter;
      Iter = AAcode.find(aa);
      if ( Iter == AAcode.end( ) ){
        cerr << "Invalid amino acid: "<< aa << endl;
        exit(1);
      }
     
      return AAcode[aa]; //returns 0 if aa code is invalid 
    }
    
    double entropy(const vector<string> &seq)
    {
        int n = seq.size();
        int k = seq[0].length();
        vector<double> et(k,0);
        int i;
        for(i=0;i<k;i++)
        {
            vector<double> p(20,0);
            int j;
            for(j=0;j<n;j++)
            {
                p[p1_to_num_MJ(seq[j].substr(i,1))]++;
                                                                
            }
                                            //cout << "bp0" << endl;
            for(j=0;j<20;j++)
            {
                                                                //cout << j << "\t" << p[j] << endl;
                p[j] = p[j]/n;
                if(p[j] == 0 || p[j] == 1)
                {
                }
                else
                {
                    et[i] += (-(p[j]*log(p[j])));
                }
            }
             /////////***********************DEAD HERE********************////                               
    
        }
                        //cout << "bp0.5" << endl;
        double sum=0;
                        //cout << "bp1" << endl;
        for(i=0;i<k;i++)
        {
            sum += et[i];
        }
                        //cout << "bp1.5" << endl;
        sum /= k;
                        //cout << "bp2" << endl;
        return sum;
    }
    
    int main()
    {
        int node = 1;
        int pop = 1000;
        int start = 1851;
        int end = 3951;
        
        string ofile;
        ostringstream ofile_os(ofile);
        ofile_os << "seq_entropy_" << node << "_" << pop << "_" << start << "_" << end << ".txt";
        ofile = ofile_os.str();
        ofstream out(ofile.c_str());
        
        int i;
        for(i=start;i<=end;i+=50)
        {
            cout << "current generation: " << i << endl;
            vector< vector<string> > seq;
            seq.resize(10);
            int j;
            string file;
            ostringstream file_os(file);
            file_os << "RUN1.gen" << setw(10) << setfill('0') << i << ".snap_sex." << setw(3) << setfill('0') << node << ".a";
            file = file_os.str();
            ifstream in(file.c_str());
            if(in.is_open()) 
            {
                string line;
                while(getline(in,line))
                {
                                                                                    //cout << line << endl;
                    istringstream instr(line);
                    string field_1;
                    instr >> field_1;
                    if(field_1 == "G")
                    {
                        int f2,f6,f7;
                        double f3, f4, f5;
                        string nseq;
                        instr >> f2 >> f3 >> f4 >> f5 >> f6 >> f7 >> nseq;
                        seq[f2].push_back(nseq);
                                                                                                        
                    }
                }
                in.close();
            }
            else
            {
                cerr << "file not exist: " << file << endl;
                exit(1);
            }
            out << i << "\t";
                                            //cout << "bp1" << endl;
            for(j=0;j<10;j++)
            {
                                                                
                cout << "seq_num: " << j << endl;
                out << entropy(seq[j]) << "\t";
            }
            out << endl;
        }
        out.close();
        return 0;
    }
    Last edited by newbinbin; 09-27-2012 at 01:44 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Do you have an example file? I'd like to help you debug but I can't find the error without everything you are using.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    example file

    The sample file is an ascii file with filename: RUN1.gen0000000851.snap_sex.001.a

    The size of the file is 8.5MB and the content is as follows: (The true file has this content repeating 1000 times)

    0 0 1000

    C 1 1 1.000000e+00 1.000000e+00 9.980151e-01 1.044513e-312 1.000000e+00 5.000000e-01 0.000000e+00 2.121997e-314 10
    G 0 1.00 1.00000000 -9.14620000 0 0 THKLILAGNWKMHKTITEAKKYVSLLINALHDVKEFEIVVCPPSTALSEV GGILSVRNIKLGAQNVYYEDKGAYAGEISPLMLQEIGVEYVIVGQSERMR IFKEDDEFISRKVKAVLEKGMTPILCVGETLEEREKGLTFCVVEKQVREG FYGLDKEEAKRVVVAYEPVWAIGTGRVATPPLAQGVHAYIRKLLSEMYDE ETAESIRILYGGSIKPDNYLGLIVQEDIDGGPVGGASLKEAFIELALIMR GV ACTCATAAGCTGATCCTCGCTGGGAACTGGAAGATGCATAAAACGATCAC GGAAGCGAAAAAGTATGTGTCGCTGCTCATTAACGCACTTCACGACGTTA AAGAGTTCGAAATAGTGGTTTGTCCTCCGTCCACAGCTCTATCTGAAGTG GGGGGAATACTCTCTGTTAGAAACATCAAATTGGGAGCTCAAAACGTTTA CTACGAAGACAAGGGAGCGTACGCCGGGGAGATCTCTCCTCTTATGCTGC AAGAGATCGGCGTTGAGTACGTGATCGTGGGACAATCAGAGAGAATGCGT ATTTTCAAAGAAGACGATGAGTTCATAAGTAGGAAAGTCAAAGCGGTGCT TGAGAAAGGTATGACTCCTATTCTCTGCGTTGGAGAAACACTAGAGGAAA GAGAGAAAGGGCTCACTTTCTGCGTTGTGGAAAAACAGGTGAGAGAAGGT TTCTACGGTCTCGACAAAGAGGAAGCAAAGAGAGTGGTAGTAGCTTACGA GCCAGTCTGGGCAATCGGGACAGGAAGGGTGGCGACTCCACCGCTGGCAC AGGGAGTCCATGCGTACATAAGAAAGCTGCTTTCAGAGATGTACGACGAG GAAACAGCGGAATCGATAAGGATTCTCTACGGTGGAAGCATAAAGCCGGA CAATTACCTCGGTCTCATCGTTCAGGAGGATATAGATGGTGGTCCCGTTG GAGGAGCCAGTCTCAAAGAGGCTTTCATAGAACTTGCACTAATAATGAGA GGTGTG
    G 1 1.00 1.00000000 -5.36600000 0 0 SSVYKIALGIEYDGSRYYGWEGQNEVRSVQEMLEKALSQVANEPITVSCA GRTDAGVHGTGQVVHFETTALRKDAALTLGVNANLPGDITVRWVQTVPDD FYARFSATARRYRYIIYNQRLRQAVLSKGVTHLYEPLDAERMHRATQCLL GENDFTSFRAEKCQSRTTWRKVMHIKVTRDGPYVVVDIKANAFVLSMVRN IVGSLMEVGAHNQPESWIAELLTAKVTTLAAATASAEGLYLVAVDYPDRY DLPKPTVGPLFLAD TCGTCAGTTTATAAAATTGCGCTGGGCATTGAGTACGACGGCAGTAGGTA TTACGGCTGGGAAGGGCAGAACGAAGTCCGCAGTGTACAGGAGATGCTGG AAAAGGCGCTCTCCCAGGTGGCGAACGAACCCATCACCGTCTCCTGCGCT GGGCGTACTGACGCAGGGGTACACGGTACCGGGCAGGTTGTGCATTTCGA AACTACCGCGCTGCGCAAAGACGCGGCGTTGACCTTGGGCGTAAATGCGA ATTTACCGGGTGACATCACTGTGCGTTGGGTTCAAACTGTACCTGATGAT TTTTATGCCCGATTTAGCGCCACGGCTCGCCGTTATCGCTACATCATCTA CAATCAGCGGCTGCGCCAGGCGGTACTGAGTAAAGGGGTAACCCATCTTT ACGAACCGCTCGACGCTGAACGGATGCATCGGGCTACGCAATGCTTGCTG GGCGAGAATGATTTCACCTCCTTCCGTGCGGAGAAGTGCCAGTCCCGAAC CACGTGGCGCAAAGTTATGCATATTAAAGTCACGCGTGACGGTCCTTATG TGGTGGTAGATATTAAAGCAAATGCTTTTGTTCTTTCTATGGTCAGGAAT ATTGTCGGCAGTCTGATGGAAGTCGGTGCCCACAACCAGCCAGAGAGCTG GATAGCAGAGTTGCTGACGGCAAAGGTCACAACCCTTGCGGCAGCAACGG CAAGTGCGGAAGGGCTGTATCTGGTCGCGGTGGATTACCCTGACCGGTAT GATCTTCCAAAACCGACAGTGGGCCCGCTATTTCTGGCGGAC
    G 2 1.00 1.00000000 -4.03940000 0 0 LTWLDVLAPEKQQPFFLNTLQTVANERHSGVPIYPPQKDVFNAFRFAELG DLKVVILGPDPYHGPGQAHGLAFSVRLGIGIPPSLLNMYTLLEKTIPGFT RPDHGYLQSWARQGVLLLNTVLTVRAVQAHSHASLGWETFTDKVISLINQ HREGVVFLLWGSHAQKKGAILDKQRHHVPKAPHPSPLTAHGGFFGCNHFV LANKWLEQHGETPIDWIPVLPAESE TTAACCTGGCTTGACGTGCTGGCTCCAGAGAAGCAGCAACCCTTCTTTCT TAATACCCTTCAGACCGTCGCCAACGAGCGGCATTCCGGCGTCCCTATCT ACCCACCACAAAAAGATGTCTTTAACGCGTTCCGATTTGCAGAGTTGGGT GACCTTAAGGTGGTGATTCTCGGCCCGGATCCTTATCACGGACCGGGACA GGCGCACGGTCTTGCATTTTCCGTTCGTCTCGGCATTGGCATTCCTCCAT CATTATTGAATATGTATACACTGCTGGAAAAAACTATTCCGGGCTTCACC CGCCCTGATCATGGTTATCTTCAAAGCTGGGCGCGTCAGGGCGTTCTGCT ACTCAATACTGTGTTGACGGTACGCGCAGTACAGGCGCATTCTCACGCCA GCCTCGGCTGGGAAACCTTCACCGATAAAGTGATCAGCCTGATTAACCAA CATCGCGAAGGCGTGGTGTTTTTGTTGTGGGGATCGCATGCGCAAAAGAA AGGGGCGATTCTAGATAAGCAACGCCATCATGTACCGAAAGCACCGCATC CGTCGCCGCTTACGGCGCATGGTGGATTCTTTGGCTGCAACCATTTTGTG CTCGCAAATAAGTGGCTGGAGCAACATGGCGAGACGCCGATTGACTGGAT TCCAGTACTACCGGCTGAGAGTGAG
    G 3 1.00 1.00000000 -8.11470000 0 0 ASTADIISQLKKLSLAESAVAKDSHPDVNIVDLMRNYISQQLSKISGVDS SLIFAALEWTNNMESGDLLIPIPRLKIKSANPKDLAVQWSEKFPCGDFLE KVVANGTLIQFFFNPQFLAKLVIPAILTRKEGYG GCTAGCACAGCAGATATAATTTCTCAACTAAAGAAACTATCTCTCGCGGA ATCTGCGGTTGCCAAGGATTCTCATCCTGATGTAAATATCGTGGATTTGA TGAGAAATTACATTTCACAACAATTAAGCAAGATTTCCGGCGTTGACTCG TCTCTTATTTTTGCAGCATTAGAATGGACAAATAATATGGAGAGCGGCGA TTTGTTGATTCCTATCCCAAGATTGAAAATCAAGAGCGCCAATCCAAAGG ATTTGGCAGTCCAATGGTCTGAAAAATTCCCATGTGGAGATTTCTTGGAG AAAGTTGTAGCGAATGGCACGCTCATCCAGTTTTTCTTCAACCCTCAATT TTTAGCAAAACTTGTCATCCCAGCTATCTTAACCAGAAAGGAGGGTTATG GT
    G 4 1.00 1.00000000 -12.07210000 0 0 SCKLVINQKVIIEFSSPNIAKPFHDGHLRSTIIGALLANLYEKLGWEVIR INYLGDWGKQFGLLAVGFERYGNEEALVKEPIQHLFDVYVRINKDIEEEG DSIQLERSTNGKAREYFKRMEEGDEEALKIWKRFLEYSIEKYIDTYARLN IKFAVYSGESQVSKESMVKAMELFKEKGLTHEDKGAVLIDLIKFDKKLGK PFVQKSDGTTLYLTRDLGAAMDRYEKYHFDKMIYVIASVQDLHAAQFFEI LKQMGFEWAKYLQHVNFGMVQGMSARNGTVVFLVIILEETKAKMHEVMKK NETKYAQIEHPEEVADLVGISALMTQDMQGKRINNYEFKWERMLSFEG TCTTGCAAATTGGTTATAAACCAGAAAGTTATTATTGAATTCTCCTCTCC CAACATTGCCAAGCCGTTTCATGATGGTCATTTAAGATCCACTATTATTG GTGCCTTATTGGCCAACCTTTATGAAAAATTAGGTTGGGAGGTTATCAGA ATTAACTACCTGGGTGACTGGGGTAAACAATTTGGTCTTTTAGCTGTTGG TTTTGAACGATACGGTAATGAAGAGGCTTTAGTTAAGGAACCAATTCAGC ACCTGTTCGATGTTTACGTTCGTATCAACAAGGATATTGAAGAAGAAGGT GACAGTATTCAATTAGAACGATCCACCAATGGAAAAGCTCGTGAATATTT CAAGAGAATGGAAGAGGGTGACGAGGAAGCTTTAAAAATTTGGAAGAGAT TCCTTGAATATTCTATTGAAAAATATATTGACACATATGCACGTTTGAAC ATCAAATTTGCTGTTTACTCGGGTGAATCTCAAGTTTCTAAAGAATCCAT GGTCAAAGCGATGGAGCTGTTCAAGGAAAAGGGCTTAACACATGAGGATA AGGGTGCAGTTTTGATCGATTTGATAAAATTTGACAAAAAGTTAGGAAAG CCTTTTGTTCAAAAATCGGACGGTACTACGTTATATCTAACAAGAGACCT TGGTGCAGCTATGGATCGTTATGAAAAGTATCATTTTGACAAGATGATTT ACGTTATCGCTTCTGTACAAGATTTGCACGCTGCCCAATTCTTCGAAATT TTGAAGCAGATGGGCTTTGAGTGGGCTAAATATCTACAGCATGTCAACTT TGGTATGGTCCAAGGTATGTCAGCAAGAAACGGTACTGTTGTCTTCTTAG TTATTATCTTGGAGGAAACTAAGGCAAAAATGCACGAAGTTATGAAAAAG AATGAGACCAAATACGCGCAAATTGAGCATCCAGAAGAAGTTGCCGATTT AGTTGGTATCTCTGCCCTCATGACTCAAGATATGCAAGGTAAGCGTATCA ACAACTACGAGTTTAAATGGGAAAGAATGCTTTCATTCGAAGGT
    G 5 1.00 1.00000000 -9.20620000 0 0 ETGPYLQFAHSRLRSVERNASGITQEKWKNADFSLLKEPAAKLLIRLLGQ YPDVLSNAIKTHEPTTLVTYLLKLTEQVSSCDDVLRVAGQTEELATARLA LYGAARQVLYNGMRLLGLPPLERM GAAACCGGTCCATACCTTCAATTTGCTCACTCAAGGTTAAGGTCAGTTGA AAGAAATGCTTCTGGTATCACCCAAGAAAAATGGAAAAATGCCGATTTTT CGTTATTAAAAGAACCTGCCGCAAAACTGTTGATTAGACTGCTAGGCCAA TACCCTGATGTTTTGAGCAATGCAATTAAAACCCACGAGCCAACAACCCT GGTCACATATTTATTGAAATTGACTGAACAGGTGTCTTCTTGTGATGACG TCTTAAGGGTTGCTGGTCAAACTGAAGAATTAGCAACTGCCCGTCTGGCT CTATATGGTGCTGCAAGACAAGTTTTATACAACGGTATGCGCTTGTTAGG TCTACCTCCCTTAGAAAGAATG
    G 6 1.00 1.00000000 -8.83090000 0 0 TIISTKYLLQDAQSNGYAVPAFSIHNAKTIQAILEVCSEMRSPVILAGTP GTFKHIPLEEIYALCSAYSTTHNLPLALHLDNHESLDDIRRKVHAGVRSA MSDASHFPFAENVKVVKSVVDFCQSQECSVEGQLGRLGGVEDDISVDAES AFLTDPQGAKRYVELTGVDSLAVAIGTALGLYSKTPKIDFQRLAEIREVV EVPLVLHGASDVPDEFVRRTIELGVTKVNVAIELKIAFAGAVKAWFAENP QGNDPRSYMRVGMDAMQEDVRNKINVCGSANRIP ACCATTATCTCCACTAAATATCTGTTACAGGACGCCCAGTCCAATGGCTA CGCGGTGCCTGCTTTTAGCATTCATAACGCCAAGACGATCCAAGCGATCC TCGAAGTGTGCAGTGAAATGCGATCGCCGGTGATCCTCGCCGGAACGCCG GGGACCTTTAAACACATCCCGCTGGAAGAGATCTACGCCCTGTGTAGCGC CTATTCCACAACACACAACCTGCCACTGGCGCTGCATCTCGACAACCACG AATCGCTGGATGATATTCGCCGTAAAGTCCACGCAGGTGTGCGCAGTGCG ATGAGCGACGCCAGCCACTTCCCGTTTGCCGAGAACGTGAAGGTGGTGAA ATCGGTTGTTGACTTCTGCCAATCACAAGAGTGCAGCGTGGAAGGACAAC TGGGCCGTCTGGGCGGTGTAGAAGATGACATTAGCGTTGACGCCGAAAGT GCATTCCTGACCGATCCACAAGGAGCTAAACGCTATGTCGAACTGACTGG GGTCGACAGCCTGGCGGTAGCGATTGGTACCGCGCTCGGCTTATACAGCA AAACGCCGAAGATTGATTTCCAGCGGCTGGCGGAAATTCGTGAAGTGGTG GAAGTTCCTCTGGTGCTGCATGGTGCCAGCGATGTTCCGGATGAATTTGT CCGTCGCACCATTGAACTTGGCGTCACAAAAGTGAACGTTGCCATAGAAT TAAAAATAGCCTTCGCTGGCGCGGTTAAAGCCTGGTTTGCGGAAAATCCG CAGGGTAATGATCCTCGTTCTTATATGCGCGTCGGAATGGATGCAATGCA AGAAGATGTGAGAAATAAAATTAATGTTTGTGGTTCAGCCAATCGAATTC CA
    G 7 1.00 1.00000000 -4.44930000 0 0 RMRKGIILAGGSSTRLYPVTSAVSKQLLPIYDKPMIYYPLSTLMLARIRD ILIISTAQDTPRFQQMLGDGSQWGLNLQYKEQASPDGLAQAFIVGEVFIG GDDCALVLGDNIFYGLDLPKLMKDAVNKESGSTVFVYHVNNPERYGVVEF DKNGTGISLEEKPLEPKSNYGVTGLYFYDNDVVQMAKSLKPSARGELEIT DINRIYLEQGRLTVAMMGRRYAWLDTGTHQSLIEASDFFATIEDRQGLNV SCPEEIAFRKDFIDVEQVRKLAITLRKNNYGQYLYKMTNN AGAATGCGTAAAGGTATTATTTTAGCGGGTGGTTCTAGTACACGTCTTTA TCCTGTGACTAGCGCTGTCAGTAAACAGCTATTACCTATTTATGATAAAC CGATGATCTATTACCCGCTCTCTACACTGATGTTGGCGCGTATTCGCGAT ATTTTGATTATCAGTACAGCTCAGGATACTCCTCGTTTTCAACAAATGCT AGGTGACGGTAGCCAGTGGGGCCTGAATCTTCAGTACAAAGAGCAAGCGA GCCCAGATGGCCTAGCGCAGGCATTTATCGTCGGAGAAGTGTTTATTGGT GGTGATGATTGTGCTTTGGTTCTTGGTGATAATATCTTTTACGGTCTCGA TCTGCCGAAGCTAATGAAGGACGCAGTTAACAAAGAAAGTGGTTCAACGG TATTTGTCTATCACGTTAATAATCCAGAACGCTATGGTGTCGTTGAGTTT GATAAAAACGGTACGGGAATCAGTCTGGAAGAAAAACCGTTAGAACCAAA GAGTAATTACGGCGTTACAGGTCTGTACTTTTATGATAACGACGTAGTAC AGATGGCGAAAAGCTTGAAGCCGTCTGCACGTGGTGAGTTAGAAATTACA GATATTAACCGTATTTATCTTGAGCAGGGACGTCTAACTGTCGCGATGAT GGGGCGTCGCTACGCGTGGCTGGACACGGGGACTCATCAGAGTCTGATAG AAGCAAGTGATTTTTTTGCGACAATTGAAGACCGCCAGGGATTGAATGTT TCCTGTCCTGAAGAGATTGCATTTCGTAAAGATTTTATTGATGTTGAGCA AGTAAGGAAATTAGCTATAACACTAAGAAAGAATAATTATGGGCAGTATC TTTATAAAATGACGAATAAT
    G 8 1.00 1.00000000 -5.96730000 0 0 MNIGLLEALDQLDEEKGISKEEVIPILEKALVSAYSKNFGNSNNVEVVID RNTGNIKVYQLLEVVEEVEDTDTQISLEEAKKIDPLAKVGSIVKKELHVM NFGRIAAQTAKQVLIQRSRELEKKKQ ATGAACATAGGATTGCTGGAAGCCCTCGACCAGCTGGATGAAGAAAAAGG AATTTCCAAAGAAGAGGTCATTCCTATCCTTGAAAAAGCACTGGTGAGCG CTTACAGCAAGAACTTTGGAAATTCCAACAACGTGGAGGTCGTTATAGAT AGGAACACGGGAAACATAAAAGTGTATCAGCTCCTCGAAGTTGTGGAAGA AGTGGAAGATACAGATACACAGATATCTCTCGAGGAGGCCAAAAAGATCG ACCCCCTCGCGAAAGTTGGGTCTATTGTGAAGAAGGAACTCCACGTTATG AATTTTGGAAGAATAGCCGCGCAGACGGCGAAGCAGGTTCTCATTCAGAG AAGCAGAGAACTCGAGAAGAAAAAACAG
    G 9 1.00 1.00000000 -5.07060000 0 0 FEKDSELQGTVTTAEVIRVMGEWADIRIGKLETRLPKKEWIPGEEIKPDD VVKVYIIDVVKTTKGPKILVRR TTCGAGAAGGATTCCGAACTCCAAGGAACGGTTACAACCGCTGAAGTCAT AAGAGTCATGGGAGAGTGGGCAGACATCAGAATAGGAAAGCTCGAGACAA GGCTTCCAAAGAAAGAGTGGATTCCCGGGGAGGAAATCAAACCCGATGAT GTGGTGAAGGTCTACATCATCGATGTGGTTAAAACAACCAAGGGGCCGAA GATACTCGTGCGCAGG



    Quote Originally Posted by whiteflags View Post
    Do you have an example file? I'd like to help you debug but I can't find the error without everything you are using.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    OK, I was not able to reproduce your problem, sadly. Had there been heap corruption on my end my debugger would have screamed at me. Didn't happen.

    As I was watching the program execute I saw a couple things that worried me.

    Code:
            //cout << "bp0" << endl;
            for(j=0; j<20; j++)
            {
                //cout << j << "\t" << p[j] << endl;
                p[j] = p[j]/n;
    
                if(p[j] == 0 || p[j] == 1)
                {
                }
    
                else
                {
                    et[i] += (-(p[j]*log(p[j])));
                }
            }
    You may recognize this - it's part of the entropy function starting on line 65. I spent too much time of the program's run in the colored area of the code. Something about that doesn't feel right.

    I suspect that the input was not correct. You ought to make a contrived input file, something I can reliably test, basically, and attach a zip file here so that I can produce your error.

    In any case, I'm attaching my results too.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,425
    The symptom you describe is a classic hint that somewhere in your code an attempt is made to go outside bounds of a container. For example, accessing container[10] when container only contains 10 elements.

    Bear in mind that, when a program crashes, the culprit can be in any code executed before that point. Crashes are not necessarily immediate.

    You're using lots of ints as indices into vectors. That's not wrong, but only if those integral values are valid indices. You perform very few checks that the indices are valid (eg if a vector has size() 10, then 10 is an an invalid index).

    Depending on content of your file (which is not exactly a clean and intuitive format) there are many possibilities for your code to access vector/set/string elements using an invalid indices.

    I therefore suggest a first step to finding your problem is to check every index you can. For example, make sure that an index into a vector is between 0 and that_vector.size() - 1, before accessing the element.

    It would probably help if, when reading the file, you perform bounds checking where possible on the input, rather than just bundling data into vectors or strings.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Along the same lines as grumpy's comment; line 60 looks a bit suspect to me:
    Code:
      p[p1_to_num_MJ(seq[j].substr(i,1))]++;
    Does seq[j].substr(i,1) ever equal "X"?, 'cos if it does then splitting that line up into its constituent parts...
    Code:
      seq[j].substr(i,1) == "X"
      p1_to_num_MJ( "X" ) == -1
      p[-1] == Oh Dear!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double free or corruption
    By MaTi in forum C Programming
    Replies: 4
    Last Post: 01-24-2012, 04:07 PM
  2. Help: double free corruption??
    By GOGO1104 in forum C Programming
    Replies: 7
    Last Post: 09-28-2010, 01:12 PM
  3. double free or corruption (out)
    By Mouser58907 in forum C Programming
    Replies: 5
    Last Post: 02-24-2009, 11:20 PM
  4. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  5. double free or corruption???
    By hwttdz in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2006, 03:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21