Program crashes for an unknown reason .

HI!
I have a function which inputs information from cin and outputs them on file stream .
This is a part of the function :
1
2
3
4
5
6
7
8
9
...
//Function : AddNewStudentToFile (from "StudentFuncs.h")
//StudentStreamOutput is the file I'm writing information to it
//GetFirstName and GetLastName return char * 
    StudentsStreamOutput << endl << TmpStudent.GetFirstName() << endl << TmpStudent.GetLastName () << endl ;
    StudentsStreamOutput.close () ;
    cout << "TEST" ;
    return 1 ;
}


The "TEST" output is shown on the console correctly and the information I enter are written to file , so I think nothing's wrong with my algorithm , but after the function returns and while getting back to main , program crashes (Has stopped working) and this is my main :
1
2
3
4
5
6
7
8
#include "StudentFuncs.h"

int main ()
{
    AddNewStudentToFile() ;
    cout << "TEST" ;
    return 0 ;
}

The "TEST" here is NOT shown on the screen .
I really can't understand if it is my AddNewStudentToFile function or something's wrong with linking .

And in this program I'm using char * (C-Style String) in my Student class . For inputs I get strings and then 'strcpy' them to my memory allocated char * .
like this :
1
2
3
4
5
void Student :: SetFirstName (string input)
{
    FirstName = new char [input.size ()] ;
    strcpy (FirstName , input.c_str ()) ;
}

As this is my first experience with C-Strings , can that be a problem ?
Thanks you in advance!
OP said:
//GetFirstName and GetLastName return char *
Are you remembering to make them static?
Never mind I didn't noticed that they are member functions.

EDIT: Can we see the whole function?
Last edited on
This may not solve your problems. However, you need to allow an extra character for the null terminator of a c-string.
FirstName = new char [input.size () + 1] ;
also, if SetFirstName was called more than once, there would be a memory leak.
Thanks @Chervil
It is solved .
I just can't get it , because I used "cout" for myself to see if the members are set correctly , And they were correct .
Thanks , I totally forgot to +1 for null-terminating charachter .



I guess the reason is that , when the function returns ,; it tries to delete the temporary information and pointers it was working on , BUT as there are two extra '\0's the program can't handle them (still not sure and don't know why)

and @Computergeek01 , in the case of being curious :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int AddNewStudentToFile () //Get a new student's informations and saves them to student.txt
{
    fstream StudentsStreamInput ("student.txt" , ios::in) ;
    vector <string> FileLines ;
    int Q ;
    StudentsStreamInput >> Q ;
    string TmpString ;
    while (!StudentsStreamInput.eof())
    {
        getline (StudentsStreamInput , TmpString) ;
        FileLines.push_back (TmpString) ;
    }
    StudentsStreamInput.close () ;

    Student TmpStudent;
    cout << "\n\nNew Student :\n\nFirst Name : " ;
    cin >> TmpString ;
    TmpStudent.SetFirstName (TmpString) ;
    cout << "Last Name : " ;
    cin >> TmpString ;
    TmpStudent.SetLastName (TmpString) ;
    cout << TmpStudent.GetFirstName() << endl << TmpStudent.GetLastName() ;
    fstream StudentsStreamOutput ("student.txt" , ios::out) ;
    StudentsStreamOutput << Q + 1 ;
    vector <string> :: iterator it ;
    for (it = FileLines.begin () ; it != FileLines.end () ; it ++)
    {
        StudentsStreamOutput << * it << endl ;
    }
    StudentsStreamOutput << endl << TmpStudent.GetFirstName() << endl << TmpStudent.GetLastName () << endl ;
    StudentsStreamOutput.close () ;
    cout << "TEST" ;
    return 1 ;
}


(Sorry still not commented)
Well, I assume the strcpy() would have to put the terminating zero somewhere. In doing so, it would overwrite some adjacent byte of memory. The result of doing that is unpredictable, but it looks as though some important data was corrupted.
Topic archived. No new replies allowed.