Your vectror::push_back() crashes....

have you ever experienced program crashes with push_back() function?
how you solved it?
do you know why it happened or when it can happen.
now in my program it crashes, and i can not realize why....
thanks :)
The only ways a push_back would crash is:

1) out of memory (very unlikely)

2) The constructor of the object being copied throws an exception (ie, if you have vector<MyClass>, MyClass's ctor throwing an exception would screw you up if you aren't catching it).

3) Your vector object is bad/corrupt (ie, trying to do somevector->push_back(x); when somevector is a null/bad pointer.
so if debug shows this:
warning: can't find linker symbol for virtual table for `MyClass' value

what does it mean?
its constructor is very simple:

1
2
3
4
5
6
7
MyClass::MyClass(std::string name, unsigned short int password, unsigned int money):MyOtherClass(name)
{
  user_password=password;
  user_money=money;
  user_cash=0;
  std::cout<<"hey\n";
}


guess it does not throws exceptions...
oh sorry, forget, and this

1
2
3
4
5
6
MyOtherClass::MyOtherClass(std::string name)
{
  user_name=name;
  srand(time(NULL));
  user_id=rand()%1000000+1;
}
4) The destructor, copy constructor and assignment operator are not properly implemented. (only if you do need to implement them)
Ok...
push_back() function crash when program read vector from file... if program call push_back() function without loading vector from file all works perfectly...
so if vector is empty, i can add as much elements as i want to the vector by calling same push_back() function, but if in the vector there are some elements already loaded from file, calling same push_back() function making it crash....
very strange, isn't it?
How are you reading from the file?

Please tell me you're not doing something like this...

1
2
vector<foo> myvec;
myfile.read( (char*)&myvec, sizeof(myvec) );


Because that will not work. (This falls under #3, bad/corrupt vector)
here it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void User_ROOT::read_data(std::vector<User_Ordinary> &k)
{
  std::ifstream file_reader_1("bb_database.dat");
  int size1;

  if(!file_reader_1.is_open())
    {
      std::cout<<"Database file could not be opened. Maybe it doesn't exist !\n";
    }
  else
    {
      file_reader_1.read((char *)&size1, sizeof(size1));
      k.resize(size1, User_Ordinary("username", 1000, 0));
      file_reader_1.read((char *)&k[0], size1*sizeof(User_Ordinary));
      file_reader_1.close();
      std::cout<<"Data read successfully !!!\n";
    }
}
does User_Ordinary contain any complex types (like string)?

If yes, that's your problem.
sure....
User_Ordinary("username", 1000, 0)

"username" is string !

1
2
3
4
5
6
7
8
User_Ordinary::User_Ordinary(std::string name, int password, int money):User_empty(name)
{
  // TODO Auto-generated constructor stub
  user_password=password;
  user_money=money;
  user_cash=0;
  std::cout<<"hey\n";
}


and

1
2
3
4
5
6
User_empty::User_empty(std::string name)
{
  user_name=name;
  srand(time(NULL));
  user_id=rand()%1000000+1;
}
Soooo....???!!!
what i should to do?
change string to char array?

i noticed many problems with STL vectors,,,, and this one.... hahahha
give me an advice, please, im novice programmer, and disappointed by this
ok,
replaced std::string user_name; with char user_name[30];
all works now.
thank you very much Disch !!! :-)

but nevertheless ... STL sucks,,,, :(
You'll need to write an actual operator >> for your class that loads the class. It basically boils down to you are trying to save pointers without saving what you are pointing to.
Last edited on
Yeah, the problem is not with std::vector but in how you use it.
Topic archived. No new replies allowed.