Noob programmer(again) getting "double free or corruption (out)" error

This is a discussion on Noob programmer(again) getting "double free or corruption (out)" error within the C++ Programming forums, part of the General Programming Boards category; So I'm back. I've made some good progress on the program and getting close to finishing. So, now I'm trying ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    16

    Noob programmer(again) getting "double free or corruption (out)" error

    So I'm back. I've made some good progress on the program and getting close to finishing.

    So, now I'm trying to dynamically allocate some arrays to hold to test scores. I am assigning all values in these arrays to zero. After I get them then to function properly I will uncomment setScore and allow the user to input the test scores.

    Here is my code:

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    class Student
    {
    private:
        string name;
        int id;
        int * test;
        int num;
        void makeArray()
        {
            test = new int[num];
    
            for (int i = 0 ; i < num ; i++)
            {
                test[i] = 0;
                cout << test[i] << endl;
            }
    
            cout << "making array...\n";
        }
    
    public:
        Student()
        {
            cout << "Calling set functions...\n";
            setName("None");
            setID(10);
            num = 3;
            makeArray();
            cout << "creating student...1\n";
    
        }
    
        Student(int n)
        {
            cout << "Calling set functions...\n";
            setName("None");
            setID(10);
    
            if (n > 0)
                num = n;
            else
                num = 3;
    
            makeArray();
    
            cout << "creating student...2\n";
        }
    
        Student(string nm, int i, int n)
        {
            cout << "Calling set functions...\n";
            setName(nm);
            setID(i);
    
            if (n > 0)
                num = n;
            else
                num = 3;
    
            makeArray();
    
            cout << "creating student...3\n";
        }
    
        void setName(string nm)
        {
            cout << "\nThe Name: ";
            cin >> nm;
            name = nm;
    
        }
    
        void setID(int i)
        {
            bool validInput;
            do
            {
    
                cout << "\nThe ID: ";
                cin >> i;
                cout << endl;
                if ( i >= 10 && i <= 99)
                {
                    id = i;
                    validInput = true;
                }
                else
                    {
                        cout << " Invalid.   Can not set id to " << i << " for student " << getName();
                        cout << endl;
                        id = 10;
                        validInput = false;
    
                    }
            } while(!validInput);
    
        }
    
    //    void setScore(int i, int s)
    //    {
    //
    //    }
    
        string getName() const
        {
            return name;
        }
    
        int getID() const
        {
            return id;
        }
    
        void showScore()
        {
            cout << "showing score";
    
        }
    
        void display()
        {
            cout << "showing display";
    
        }
    
        ~Student()
        {
            cout << "Freeing dynamicly allocated memory...\n";
            delete [] test;
    
        }
    
    };
    
    int main()
    {
        cout << "In main\n";
        Student studentA,
                studentB(4),
                studentC("Joe",40,5);
    
        cout << "\nThe Name: ";
        cout << studentA.getName() << endl;
        cout << "The ID: ";
        cout << studentA.getID() << endl;
    
        cout << "\nThe Name: ";
        cout << studentB.getName() << endl;
        cout << "The ID: ";
        cout << studentB.getID() << endl;
    
        cout << "\nThe Name: ";
        cout << studentC.getName() << endl;
        cout << "The ID: ";
        cout << studentC.getID() << endl;
    
        studentA.~Student();
        studentB.~Student();
        studentC.~Student();
        return 0;
    
    }
    This is output so far:

    Code:
    In main
    Calling set functions...
    
    The Name: Tom 
    
    The ID: 20
    
    0
    0
    0
    making array...
    creating student...1
    Calling set functions...
    
    The Name: John
    
    The ID: 30
    
    0
    0
    0
    0
    making array...
    creating student...2
    Calling set functions...
    
    The Name: Joe
    
    The ID: 40
    
    0
    0
    0
    0
    0
    making array...
    creating student...3
    
    The Name: Tom
    The ID: 20
    
    The Name: John
    The ID: 30
    
    The Name: Joe
    The ID: 40
    Freeing dynamicly allocated memory...
    Freeing dynamicly allocated memory...
    Freeing dynamicly allocated memory...
    Freeing dynamicly allocated memory...
    *** glibc detected *** /home/john/bin/lab9/Lab9: double free or corruption (fasttop): 0x000000000098e140 ***
    ======= Backtrace: =========
    /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fc267718b96]
    /home/john/bin/lab9/Lab9[0x4016cf]
    /home/john/bin/lab9/Lab9[0x401067]
    /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fc2676bb76d]
    /home/john/bin/lab9/Lab9[0x400d29]
    ======= Memory map: ========
    00400000-00402000 r-xp 00000000 00:15 43647168                           /home/john/bin/lab9/Lab9
    00601000-00602000 r--p 00001000 00:15 43647168                           /home/john/bin/lab9/Lab9
    00602000-00603000 rw-p 00002000 00:15 43647168                           /home/john/bin/lab9/Lab9
    0098e000-009af000 rw-p 00000000 00:00 0                                  [heap]
    7fc26739e000-7fc267499000 r-xp 00000000 08:01 56102364                   /lib/x86_64-linux-gnu/libm-2.15.so
    7fc267499000-7fc267698000 ---p 000fb000 08:01 56102364                   /lib/x86_64-linux-gnu/libm-2.15.so
    7fc267698000-7fc267699000 r--p 000fa000 08:01 56102364                   /lib/x86_64-linux-gnu/libm-2.15.so
    7fc267699000-7fc26769a000 rw-p 000fb000 08:01 56102364                   /lib/x86_64-linux-gnu/libm-2.15.so
    7fc26769a000-7fc26784f000 r-xp 00000000 08:01 56102322                   /lib/x86_64-linux-gnu/libc-2.15.so
    7fc26784f000-7fc267a4e000 ---p 001b5000 08:01 56102322                   /lib/x86_64-linux-gnu/libc-2.15.so
    7fc267a4e000-7fc267a52000 r--p 001b4000 08:01 56102322                   /lib/x86_64-linux-gnu/libc-2.15.so
    7fc267a52000-7fc267a54000 rw-p 001b8000 08:01 56102322                   /lib/x86_64-linux-gnu/libc-2.15.so
    7fc267a54000-7fc267a59000 rw-p 00000000 00:00 0 
    7fc267a59000-7fc267a6e000 r-xp 00000000 08:01 56102347                   /lib/x86_64-linux-gnu/libgcc_s.so.1
    7fc267a6e000-7fc267c6d000 ---p 00015000 08:01 56102347                   /lib/x86_64-linux-gnu/libgcc_s.so.1
    7fc267c6d000-7fc267c6e000 r--p 00014000 08:01 56102347                   /lib/x86_64-linux-gnu/libgcc_s.so.1
    7fc267c6e000-7fc267c6f000 rw-p 00015000 08:01 56102347                   /lib/x86_64-linux-gnu/libgcc_s.so.1
    7fc267c6f000-7fc267d54000 r-xp 00000000 08:01 15998324                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
    7fc267d54000-7fc267f53000 ---p 000e5000 08:01 15998324                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
    7fc267f53000-7fc267f5b000 r--p 000e4000 08:01 15998324                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
    7fc267f5b000-7fc267f5d000 rw-p 000ec000 08:01 15998324                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
    7fc267f5d000-7fc267f72000 rw-p 00000000 00:00 0 
    7fc267f72000-7fc267f94000 r-xp 00000000 08:01 56102300                   /lib/x86_64-linux-gnu/ld-2.15.so
    7fc268171000-7fc268176000 rw-p 00000000 00:00 0 
    7fc26818f000-7fc268194000 rw-p 00000000 00:00 0 
    7fc268194000-7fc268195000 r--p 00022000 08:01 56102300                   /lib/x86_64-linux-gnu/ld-2.15.so
    7fc268195000-7fc268197000 rw-p 00023000 08:01 56102300                   /lib/x86_64-linux-gnu/ld-2.15.so
    7fff14746000-7fff14767000 rw-p 00000000 00:00 0                          [stack]
    7fff147ff000-7fff14800000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
    Aborted (core dumped)

    One thing I would like to point out: It appears to be freeing the dynamically allocated memory an extra time. I dont know if that is realted to the "double free or corruption (out)" error or not but it shoudn't be doing that.


    Thanks for all the help.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Deleted idea was wrong.

    New Idea/Question

    I am a C programmer, just learning C++; I have never seen a destructor called except for inside another destructor. Is this valid thing to do in C++?

    Code:
        studentA.~Student();
        studentB.~Student();
        studentC.~Student();
    Tim S.
    Last edited by stahta01; 11-23-2012 at 04:26 PM. Reason: Oops, I found it
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Line 19:
    Code:
                test[i] = 0;

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Quote Originally Posted by goldfish View Post
    Line 19:
    Code:
                test[i] = 0;
    I would say this line instead.

    test = new int[num];

    Note: I edited my prior post.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Oh I lead you astray. In Line 19 I am asigning a value to the contents of test[].
    I meant line 15:
    Code:
            test = new int[num];

    You know I think you might be right. I believe the destructors are called automatically when the program ends.

    I'm going to try it and get back to you.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    5,833
    The problem is that you are invoking destructors by hand
    Code:
        studentA.~Student();
        studentB.~Student();
        studentC.~Student();
    and they will be automatically invoked again as main() returns.

    Remove the three lines I've shown.

    There is a place for manually calling constructors, but this isn't it.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Yuppers that was it! Thanks for the help. I shall trek on and FINISH my program!!! Almost done! Yay!!

    Btw Cprogramming forums, you guys seem like a great community of programmers.
    This will not be the last you see of me!!!

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    This thread needs to be marked as solved. I cant seem to edit my first post and mark it as such.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Btw, if you need a dynamic array, you can use std::vector instead. Avoid raw pointers in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-23-2012, 11:33 AM
  2. "Heap corruption detected" Error
    By tmrsgv in forum C Programming
    Replies: 13
    Last Post: 06-14-2012, 02:40 AM
  3. Error: Double free or corruption (out)
    By kieserjw in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2012, 10:47 AM
  4. Replies: 3
    Last Post: 08-08-2008, 07:40 AM
  5. 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

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