struct Movie
{
char* date;
char* title;
char* rating;
int runtime;
};
typedef Movie* moviePtr;
moviePtr tempDB;
bool doStuff()
{
//... snip .,,
tempDB = new Movie[tempC];
int i;
ifs >> temp;
while(ifs.good() && i < tempC)
{
tempDB[i].date = new char[strlen(temp)+1];
strcpy(tempDB[i].date, temp);
//... snip ...
}
When I run that, I get a segmentation fault (on line tempDB[i].date = new char[strlen(temp)+1]; I debugged it)
tempC is how big the array should be, read from the file.
What have I done wrong 🙁
You did not initialize i to 0, or whatever you want to start indexing at.
Another note, in case of errors like seg-fault use Valgrind [valgrind.org] — it helps spot the error in seconds. In your case an out of index memory access — these are things Valgrind specializes in detecting.