Like Tree19Likes

Somewhere in my program I'm not freeing memory. Can't find where

This is a discussion on Somewhere in my program I'm not freeing memory. Can't find where within the C++ Programming forums, part of the General Programming Boards category; Don't be shy! We all wanted to bang Lee Majors; there is even a song about it. o_O OK, so ...

  1. #46
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Don't be shy! We all wanted to bang Lee Majors; there is even a song about it.
    o_O

    OK, so what...am I in the minority that I just wanted to BE Lee Majors?
    Elkvis likes this.

  2. #47
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Yep, now that Heather Thomas on the other hand ...

  3. #48
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Here's my new program; New AI.txt

    Here is the new function;

    Code:
    void findMatch(){
        string temp1;
        string temp2;
        string temp3;
        string temp4;
        string temp5;
        string temp6;
        string temp7;
        string temp8;
        string dataBaseSentence1;
        string dataBaseSentence2;
        string dataBaseSentence3;
        string dataBaseSentence4;
        string dataBaseSentence5;
        string dataBaseSentence6;
        string dataBaseSentence7;
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        int count5 = 0;
        int count6 = 0;
        int count7 = 0;
        int countResult = 0;
        vector<string> text_file1;
        vector<string> text_file2;
        vector<string> text_file3;
        vector<string> text_file4;
        vector<string> text_file5;
        vector<string> text_file6;
        vector<string> text_file7;
        string found;
        std::ifstream hfin1("holdSentence.txt", fstream::app), fin2("holdDatabase.txt", fstream::app);
        if (!hfin1 || !fin2) { std::cerr<<"file error\n"; std::exit(1); }
    
        getline( hfin1, temp1 );
    
        while( getline( fin2, temp2 ) ){
    
            for (string::size_type a = 0; a < temp2.length(); ++a) {
                if(temp2[a] == '4' ){
                    for (string::size_type b = 0; b < temp1.length(); ++b) {
                        if(temp1[b] == '1' ){
    
                            if (temp2.find(temp1[b]) + 1 != string::npos) {
                                count1++;
                            }
                            if(count1 > 0 && countResult == 0){
                                cout << endl << temp2 << endl;
                            countResult++;
                            }
                        }
                    }
                }
            }
    
            text_file1.push_back( temp2 );
        }
    
        hfin1.close();
        fin2.close();
    }
    Please ignore the bad naming of the variables, I'm not finished.

    Here is the program running;

    Enter your sentence, end it with a period: sunny.

    Input sentence to program, from input.txt;
    s suunny
    xxxyy 4


    Press any key to continue . . .

    _________________

    I'm trying to find common chars between the input and output string.
    Oh and your funny about the thousand trillion dollar man.

  4. #49
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    jeremy, you need not explicitly close() those ifstreams at the end of the function. Their destructors will take care of that for you.

  5. #50
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by jeremy duncan View Post
    Here's my new program; New AI.txt

    Here is the new function;

    Code:
    void findMatch(){
        string temp1;
        string temp2;
        string temp3;
        string temp4;
        string temp5;
        string temp6;
        string temp7;
        string temp8;
        string dataBaseSentence1;
        string dataBaseSentence2;
        string dataBaseSentence3;
        string dataBaseSentence4;
        string dataBaseSentence5;
        string dataBaseSentence6;
        string dataBaseSentence7;
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        int count5 = 0;
        int count6 = 0;
        int count7 = 0;
        int countResult = 0;
        vector<string> text_file1;
        vector<string> text_file2;
        vector<string> text_file3;
        vector<string> text_file4;
        vector<string> text_file5;
        vector<string> text_file6;
        vector<string> text_file7;
        string found;
        std::ifstream hfin1("holdSentence.txt", fstream::app), fin2("holdDatabase.txt", fstream::app);
        if (!hfin1 || !fin2) { std::cerr<<"file error\n"; std::exit(1); }
    
        getline( hfin1, temp1 );
    
        while( getline( fin2, temp2 ) ){
    
            for (string::size_type a = 0; a < temp2.length(); ++a) {
                if(temp2[a] == '4' ){
                    for (string::size_type b = 0; b < temp1.length(); ++b) {
                        if(temp1[b] == '1' ){
    
                            if (temp2.find(temp1[b]) + 1 != string::npos) {
                                count1++;
                            }
                            if(count1 > 0 && countResult == 0){
                                cout << endl << temp2 << endl;
                            countResult++;
                            }
                        }
                    }
                }
            }
    
            text_file1.push_back( temp2 );
        }
    
        hfin1.close();
        fin2.close();
    }
    Please ignore the bad naming of the variables, I'm not finished.

    Here is the program running;

    Enter your sentence, end it with a period: sunny.

    Input sentence to program, from input.txt;
    s suunny
    xxxyy 4


    Press any key to continue . . .

    _________________

    I'm trying to find common chars between the input and output string.
    Oh and your funny about the thousand trillion dollar man.
    You will never learn to program unless you seriously reconsider your plan of attack.
    Salem likes this.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  6. #51
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Neo1, this thread was finished, I did what I wanted to do, I fixed a memory bug and reduced the lines of code. This thread is not needed by me anymore. I don't like to pontificate, and to dos o with strangers is asking a lot from me. And that strangers ask if I'm this in my head or that is very wrong to my morals, I wouldn't do that. So yeah, strange times here indeed for me.

  7. #52
    Registered User
    Join Date
    Oct 2006
    Posts
    2,349
    jeremy, read this very carefully and try to understand it.

    any time you have numbered variables (temp1, temp2, etc), you should be using an array, or even better yet, a vector. you're not even using most of the variables that you're declaring. what do those variables represent? how do you know what they are? they need to have more descriptive names. hfin1 and fin2 mean nothing outside of your brain, and you should really call them something better - holdSentenceFile and holdDatabaseFile, for example.

  8. #53
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Elkvis,

    I woke up this morning and had an idea on how to fix a function in my program and I've been working for about twelve hours and just got home to make it, see the other thread about substr I made to see the code I just wrote. I basically saying I haven't had any time since I last posted to do anything with this AI program.

    So I updated my program with the code I posted in that other thread, and here it is in case you want to play with it; New AI.txt

    It's almost time to call it a day and now I will get ready for sleep. Tomorrow I will try again and maybe redo my program with better structure and naming. if my code in the substr thread is OK that is.

  9. #54
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    That code I posted previously didn't work as well as I had hoped, so I updated my code from the substr thread and now the updated code is in my ai program. Here is the new version; New AI.txt

    I'm just posting this as a courtesy since somebody may be playing with my code and this new findPattern function works I think.
    I will watch the substr thread to see if the function I posted today is buggy before I look at remaking my program.

  10. #55
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I started to clean up my function then had to redo the findpattern function from the substr thread. But the function is a lot smaller and tomoorow or the next few days I will try and get around tocleaning it up more then post the easier to read and understand version.

    Here is the newest version;

    AI.txt

    I'm attaching this file as a courtesy in case somebody is trying out my program, this is the best version so far.

  11. #56
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I have renamed the variables and made the functions smaller, as you suggested, but the findMatch function is difficult to shrink and I'm hoping somebody in the substr thread will help me with that.

    But here is the new AI source if you want to look at it and try it out;

    AI.txt

    Thanks for your patience with me. I'll be watching that substr thread for anything I can try here to effectively shrink that function without breaking my program.

  12. #57
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I had some good advice from Salem, and he gave me some code I used to then create my new findMatch function, its about 100 less lines of code compared to before, and it works the same.

    So here is my new program source; AI.txt

    After fixing my memory bug I tried to get my program to work properly and to reduce the number of lines of code in the program, and to rename the variables so the code can be understood better. I think I have done that so I can think of what to do next. Thank you people for you time and valuable suggestions.

  13. #58
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > this thread was finished

    And then you ........ing quintuple-post. Nice job.

  14. #59
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I thought you people were still talking about me freely. So I was talking back to you. But I guess not.

  15. #60
    Registered User
    Join Date
    Jun 2005
    Posts
    6,246
    Jeremy, I suggest you read through this thread again.

    There is quite a bit of constructive advice, concerned with things like avoid code duplication and using arrays instead of a set of numbered variables. And you have provided little or no evidence in your posts that you have understood, let alone applied, that advice.

    The links to source you have provided in your last four posts are unreadable: when I load them into notepad, they look like binary files. Very few people (probably nobody) can provide advice from that.

    Back to the subject of the thread (unable to find where memory is not being freed) the technique is to start with the code that is allocating the memory, then to find the point in code where that memory is no longer needed. That is the point where the memory needs to be freed. If the structure of your code prevents you working out where memory should be released, then you need to restructure your code to make it easier - ensure rational program flow, avoid "copy and paste" duplication of code, use small well-defined functions, etc etc. The advice from others early in this thread will help with that.
    Right 98% of the time, and don't care about the other 3%.

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  2. Am I freeing up the memory used in my program?
    By abrownin in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2010, 03:13 AM
  3. Freeing memory
    By vbdave78 in forum C Programming
    Replies: 7
    Last Post: 11-18-2009, 11:56 AM
  4. Freeing memory
    By C_ntua in forum C Programming
    Replies: 17
    Last Post: 06-29-2008, 04:42 AM
  5. Freeing memory necessary?
    By Snip in forum C Programming
    Replies: 3
    Last Post: 11-05-2005, 06:01 AM

Tags for this Thread


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