0

I managed to isolate a bug that made the program to crash but I can't figure out the issue yet. I suppose the problem is in the call to the default constructors for the objects lookProfile and lookContacts, since, once I remove those lines the program runs smoothly.

This is the main.

#include <iostream>
#include <fstream>
#include "profile.h"
#include "contact.h"
#include <sstream>

using namespace std;

void Query(profile* &lsp,int,ofstream& outt);

int main()
{
    ifstream inn;
    ofstream outt;

    char resp;

    profile *listp;

    int length=0;

    listp = new profile[50];

    inn.open("profiles.txt");     // retrieve profiles
    retrieveProfiles(inn,listp,length);
    inn.close();

    cout << "Do you want to add your profile or to look for someone? (A/L)";
    cin >> resp;

    if(resp=='A')
    {
        outt.open("profiles.txt",ios::app);
        addProfile(outt);
        outt.close();
    }else if(resp=='L')
    {
        outt.open("contacts.txt",ios::app);
        Query(listp,length,outt);
        outt.close();
    }
    else
        cout << "Wrong Input";

    return 0;
}

void Query(profile* &lsp,int length,ofstream& outt)
{
    const int THRESHOLD = 3;

    string str;
    int num,numvec[3];
    int *overthreshold;
    int countOver=0;
    char dis;
    profile lookProfile;
    contact lookContact;
}

These are the private members of the class profile and implementation of constructor and destructor:

 private:
     std::string name;
     std::string surname;
     int age;
     std::string icolor;
     int height;
     std::string homeTown;
     std::string livingTown;
     std::string *hobby;
     int lenghob;
     int ID;

profile::profile() //ctor
{
    name="";
    surname="";
    age=0;
    height=0;
    icolor="";
    homeTown="";
    livingTown="";
    lenghob=0;
    ID=0;
}

profile::~profile() //dtor
{
    delete [] hobby;
}

These are the private members of the class contact and implementation of constructor and destructor:

 private:
     int date[3];
     std::string time;
     std::string place;
     std::string address;
     std::string *keywords;
     int lengthkw;

contact::contact()      //ctor
{
    for(int i=0;i<3;i++)
        date[i]=1;

    time="12:00-13:00";
    place="";
    address="";
    lengthkw=0;
}

contact::~contact()
{
     delete [] keywords; //dtor
}

Thanks in advance.

radical
  • 4,364
  • 2
  • 25
  • 27
RDGuida
  • 546
  • 4
  • 15
  • I'm guessing your classes break the [Rule of Three](http://stackoverflow.com/questions/4172722) since you've shown us destructors but no copy-constructors or assignment operators. In fact, they don't seem to initialise `hobby` and `keywords` at all. Use `std::vector` instead of trying to juggle raw pointers. – Mike Seymour Oct 30 '14 at 17:28
  • `once I remove those lines the program runs smoothly.` It doesn't run "smoothly". Your code now has memory leaks without implementation of these two functions, and can easily be broken by a simple assignment. – PaulMcKenzie Oct 30 '14 at 17:56

1 Answers1

3

You've defined hobby and keywords to be std::string pointers, but you're not initializing them via a call to new. However, in your destructor you are calling delete against them. Since they're garbage you'll get undefined behaviour, so probably a crash.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • Thank you very much! The issue was created by that. So, for the future, do I always have to initialise a pointer even when I assume it is empty (like in this case)? – RDGuida Oct 31 '14 at 10:10
  • It's good practice to initialize it to something. If you want to delay allocating it then just set it to null (`keywords = nullptr`). If you're looking to have an array of keywords then just use a vector (`std::vector> keywords`) and this will make the memory management and pointer issues go away. – Sean Oct 31 '14 at 10:13