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

This is a discussion on Noob programmer getting "double free or corruption (out)" error within the C++ Programming forums, part of the General Programming Boards category; I'm a fresh programmer. So this is my first OOP program that I'm writing for a class. I'm getting a ...

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

    [SOLVED]Noob programmer getting "double free or corruption (out)" error

    I'm a fresh programmer.

    So this is my first OOP program that I'm writing for a class. I'm getting a "double free or corruption (out)" error.

    The program that I am writing basically makes Student objects and stores info about them like test scores(in an array), their name and ID. My teacher told us all the public and private member funtions so I started there. I wrote the basic structure and then through a bunch of cout statements to I could get it navigate through program properly. I'm slowly working it up and adding stuff little by little until I get it to do everything I need it to do.

    I commented out all the class members that I haven't gotten to yet.
    Have mercy on my sad sad code....
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    class Student
    {
    private:
        string name;
        int id;
        int * test;
        int num;
    //    void makeArray()
    //    {
    //        cout << "making array...\n";
    //
    //    }
    public:
        Student()
        {
            cout << "creating student...1\n";
            setName("None");
            setID(10);
    
        }
    
        Student(int n)
        {
            cout << "creating student...2\n";
            setName("None");
            setID(10);
        }
    
        Student(string nm, int i, int n)
        {
            cout << "creating student...3\n";
            setName(nm);
            setID(i);
        }
    
        void setName(string nm)
        {
            cout << "Setting name...\n";
            cout << "The Name: ";
            cin >> name;
    
        }
    
        void setID(int i)
        {
            cout << "setting ID...\n";
            num = 3;
            //makeArray();
    
        }
    
    //    void setScore(int i, int s)
    //    {
    //
    //    }
    
        string getName() const
        {
            cout << name << endl;
    
        }
    
        int getID() const
        {
            cout << id << endl;
        }
    
        void showScore()
        {
            cout << "showing score";
    
        }
    
        void display()
        {
            cout << "showing display";
    
        }
    
    //    ~Student()
    //    {
    //
    //    }
    
    };
    
    int main()
    {
        cout << "In main\n";
        Student studentA,
            studentB(4),
            studentC("Joe",40,5);
        cout << "Before showing names...\n";
        studentA.getName();
        cout << "after studentA.\n";
        studentB.getName();
        cout << "after studentB.\n";
        studentC.getName();
        cout << "after studentC.\n";
        return 0;
    
    }
    The error happens at this getName call in main: "studentA.getName();"

    Where am I going wrong?
    Last edited by goldfish; 11-23-2012 at 11:34 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    This may or may not be the cause of your problem, but I notice that your getName member function is declared as returning a string object, but you did not actually return a string object. You made a similiar omission for your getID member function. (EDIT: by the way, if you compile at a suitably high warning level, your compiler is likely to warn you about these potential problems.)

    Also, the nm parameter of your setName member function is not used.
    Last edited by laserlight; 11-23-2012 at 11:02 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Change getName and getId prototypes to return void.

    In the constructor with the three arguments, just assign the name with the argument.Do not call setName.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by std10093
    Change getName and getId prototypes to return void.
    Actually, I think that you should keep them as-is. Rather, they should actually return the name and id, which is then printed (or otherwise used) by the caller. If you want to print them instead, then it would make sense to rename them to printName and printId respectively, and hence using void as the return type would be sensible.

    Quote Originally Posted by std10093
    In the constructor with the three arguments, just assign the name with the argument.Do not call setName.
    I think the setName call is okay. If you wanted to get rid of it, then you might as well use the initialiser list instead of assignment.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Kewl, that fixed it laserlight. I need to be returning name in the getName function. There some other issues with my code aswell but thats for another thread another time lol.

    I will mark this as solved and start a new thread if I have any other major problems(hopefully I wont).

    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

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