Question:
C++ programming: Segmentation fault reading object pointers from a vector?
john
2012-05-11 14:21:06 UTC
I've been trying to get a program running for a class I'm in and I've been stuck on the same spot forever! I get a segmentation fault from the last line of the following code - Does anyone know why? I suppose I should also be adding "delete ap;" at the end of the while loop according to my instructor but that's giving me even more trouble on a separate program. Forgive me if it's an easy fix, its only my first semester in programming. Thanks!

#include
#include
#include
#include
using namespace std;

class Athlete
{
public:
Athlete(string, string, string, string, string);
void display();
private:
string jersey_number;
string best_time;
string sport;
string name;
string high_school;
};

Athlete::Athlete(string name2, string jersey_number2, string best_time2, string sport2, string high_school2) : name(name2), jersey_number(jersey_number2), best_time(best_time2), sport(sport2), high_school(high_school2)
{
}

void Athlete::display()
{
cout << name;
}

int main()
{
string title;
ifstream stream;
stream.open("data.txt");
string name;
vector catalog;

while(getline(stream, name, '|').eof())
{
Athlete* ap;
string jersey_number;
string best_time;
string sport;
string high_school;

getline(stream, jersey_number, '|');
getline(stream, best_time, '|');
getline(stream, sport, '|');
getline(stream, high_school, '|');

ap = new Athlete(name, jersey_number, best_time, sport, high_school);

catalog.push_back(ap);
}
stream.close();

Athlete* athlete;
athlete = catalog[1];
athlete->display();
}


The "data.txt" file is a pipe delimited file containing: (generally contains some integers but I changed them to strings to make it easier)
John|eighty|fifty
swimming|Jefferson
Jane|twenty|ten
sprinting|San Marin
Three answers:
oops
2012-05-11 15:04:18 UTC
The problem is that there are no elements in your vector, and you are calling the display function on the second one. The reason there are no elements in your vector is because your loop condition is messed up.



while (getline(stream, name, '|').eof())



eof() is going to return false if you haven't reached the end of file. On your first read, you clearly would not have reached the end of the file, so the loop is never entered. This is how I would do that loop:



    while (true)

    {

        string name, jersey_number, best_time, sport, high_school;



        getline(stream, name, '|');

        getline(stream, jersey_number, '|');

        getline(stream, best_time, '|');

        getline(stream, sport, '|');

        getline(stream, high_school, '|');



        if (!stream)

            break;



        Athlete * ap = new Athlete(name, jersey_number, best_time, sport, high_school);

        catalog.push_back(ap);

    }



Although, as you have your data file now, this will not work quite right, because your data file is not quite pipe delimited like you say. It is a combination of newline and pipe delimited. You could either fix your data file to be actually pipe delimited, or you could modify the code I gave you so that the getline called for best time and for high school uses '\n' as the delimiter instead of '|'.



And no, you shouldn't be adding 'delete ap' at the end of the loop. You're creating objects dynamically, and putting pointers to those objects in the vector so ap and the pointer in the vector both point to the same object. If you call 'delete ap', that's going to destroy the object, leaving your vector with dangling pointers. Why do you have a vector of pointers instead of simply vector? Is that an assignment requirement? Anyway, to properly clean up your objects, you need to add a loop at the end of main, after you're done with the objects, that calls delete on each pointer in the vector.
marivel
2016-04-29 03:50:26 UTC
If you are looking a highly effective way to simply help your son or daughter learn to learn while preventing any potential issues along the way then this system https://tr.im/FX4ps is everything you need.

Children Learning Reading program is separate into two areas which are both provided in split eBook s. Having bought the class you are provided instant usage of each of the eBook s which means you are able to use the program almost instantly after spending money on it.

Children Learning Reading is user friendly for you and for your youngster, with little training and games this program is ideal to teach how to read for a small child.
stockett
2016-12-02 12:28:31 UTC
Segmentation fault implies you have accomplished something incorrect including your memory administration. Are you making use of Linux (or comparable)? i think of you're, as you communicate over with it as a segmentation fault. if so, examine out making use of Valgrind, which will spotlight which strains are inflicting unusual behaviour, in case you convey mutually with -g in gcc, then execute your application with: $ valgrind ./programname It sounds like your code could be superb (as quickly as you have swapped the %s with %c) so i'm curious to appreciate as to why this might take place. one element the desire arises to observe out for is that 'length' as you have defined it particularly is an int, while sizeof() returns a size_t (despite if this shouldn't rely), and in C (opposite to C++), you do no longer might desire to solid the return of malloc. the respond above with regards to the nul-terminated string will reason issues because it writes previous the tip of para (4th index), and it would no longer be mandatory as you're making use of it as an array, no longer a string.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...