why do get segmentation fault ? help plz !!!

Discussion in 'C++' started by Adilo, Oct 28, 2009.

  1. Adilo

    Adilo New Member

    Joined:
    Oct 28, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    :disappoin
    Hello Guys
    I have been working on for hours and hours and I need help becuase I'm stuck . this program which might be super simple for you guys, basically it will read data from the file like " CPE 202 Introduction to Computer Science". and will cout to the screen.
    mine works but when I just read in CPE 202 but when I try to add the the third character, I get seg fault and it crashes...
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    using namespace std;
    
    class course
      {
        public:
         course ();//constructor
        ~course();//destructor 
          
            
        void setdept(char *);
        char * getdept();
    
        void setcode(int);
        int  getcode();
       
        void setclassD(char * );
        char * getclassD();
        void print() const ;
    
    
        private:
                
        char * dept;
        int   code;
        char * classD;
            
     };
    
    course::course()
    {
    dept = new char [20];
    classD = new char [20];
    
    classD[0] = '\0';
    dept[0]= '\0';
    code = 0;
    }
    
    course::~course()
    {
    delete [] dept;
    delete [] classD;
    dept = '\0';
    classD= '\0';
    code = 0;
    
    
    }
    
     void course::setdept(char* dpt)
     { 
     
        dept = new char [20];
        int i = 0;
        while(dpt[i]!='\0')
            {
            dept[i]=dpt[i];
            i++;
        }
        dept[i] = '\0';
    
     }
     void course::setcode(int kode)
     {
        code = kode;
     }
    
     char* course::getdept()
     {
        return dept;
     }
    int course::getcode()
    {    
        return code;
    }
    
    void course::setclassD(char * Des)
    {
    
        
        Des = new char [20];
        int i = 0;
        while(Des[i]!='\0')
            {
            classD[i]=Des[i];
            i++;
        }
        classD[i] = '\0';
    
    }
    
    char * course::getclassD()
    {
    
     return classD;
     }
    
     int main()
      {    
    //declaration 
            ifstream fin;
            int NUM=1; 
            int i;
            int kode;
               course * cptr;
            char * Des;
            char * dpt;
            
             cptr = new course [NUM];
     
     
        fin.open("course.txt");    
        
        
        for(i=0; i <NUM; i++)
        {
                    fin >>dpt;
                     (*cptr).setdept(dpt);
    
                    fin >> kode;
                    (*cptr).setcode(kode);
    
                    fin.getline( Des, 50 );
                   (*cptr).setclassD(Des);
                      
        }
    
        cout << " <Schedule> " << endl;
        cout<<endl;
    
        for(i=0; i<NUM; i++)
        {
            cout << "Course " << ": " <<(*cptr).getdept() << " "<< (*cptr).getcode() << endl;
            cout<<endl;        
            cout<< " class Des " << ":" <<(*cptr).getclassD()<<endl;    
        }         
     system("pause");    
         
        return 0;
    }
    
    
    // copy constructors
    /* 
    course::course(const course &rhs)
    {
    dept = rhs.dept;
    code = rhs.code'
    classD = rhs.classD;
     
    
    dept = new char [len+1];
    //deep copy 
    while (rhs.dept[i] ='\0')
    {
    strcopy();
    }
    }
    */
    
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    char * dpt; defines dpt as a pointer to a char, but you never initialise this pointer anywhere. So when you use it at fin >>dpt; this writes to unknown memory.
     
  3. Adilo

    Adilo New Member

    Joined:
    Oct 28, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    thanks for responding !!!
    I tried to but it doesn't work still !!! at least that what i get !!!
    the problem start when I add the classD !!!
    plz help
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How did you try to initialise dpt?
     
  5. Adilo

    Adilo New Member

    Joined:
    Oct 28, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    oh !!! I must doing it wrong then because I was declaring it char * dpt;
    and i get an error that I'm re declaring it again !!!
    seriously I'm being blind now after over 6 hours trying to see what's wrong ... !!!
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You have to set it equal to something. I don't know what you're trying to do, but at a guess you want to store some characters in memory, would that be correct? If so, how many characters? If not, what is the point of dpt and what exactly do you want to do with it?
     
  7. Adilo

    Adilo New Member

    Joined:
    Oct 28, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    when I comment out classD stuff, the program works fine, but as soon as i add it it crashes, dpt is just a temporary pointer that will store dept( department which is CPE). so when i run just the two i get CPE 202 . as soon as I add classD to get the word " introduction to computer science" the program crashes !!!
    let me know if you need more clarifications thanks for your help
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Must be the classD stuff then. Obviously I know nothing about memory corruption caused by writing to memory with an uninitialised pointer.
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Here's a thought: why not initialise dpt to zero? i.e. char *dpt=0; Then what happens?
     
  10. Adilo

    Adilo New Member

    Joined:
    Oct 28, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    :nonod: still the same issue !!!
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    On what line of code?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice