0

Good morning everyone, I currently have an issue in my code, it crashes without any error output. I was hopping you could lend me a hand!

The issue occurs when I append an element at a vector, but not at the first iteration always at the second.

Here is my main code:

/*Various includes*/
int main(void)
{
  int cmpt = 0;
  std::vector<Commande> _vct;
  Commande cmd;
  while(cmpt < 15)
  {
    cout << "..." << endl;
    _vct.push_back(cmd);
    cout <<" test cmd" << _vct[cmpt].getBytes() << endl;
    cmpt++;

    }

  return 0;
}

Here is the object's class that is appened to the vector:

Commande::Commande()
{
    bytes="";
    from="";
    type="";
    first_s="";
    last_s="";
    value=-1;
    first_i=-1;
    last_i=-1;
}

Commande::~Commande()
{

}

void Commande::toString()
{
    cout << "=-_-=> from: '" << (this)->getFrom() << "', type: '" << (this)->getType() << "', value: '" << (this)->getValue() << "', bytes: '" << (this)->getBytes() << "'." << endl;
}

void Commande::fromStringToInteger()
{
    (this)->setFirst_i(atoi((this)->getFirst().c_str()));
    (this)->setLast_i(atoi((this)->getLast().c_str()));
}

And finally the .h of Commande.cpp:

#include <cstdlib>
#include <iostream>

#ifndef COMMANDE_H_
#define COMMANDE_H_

using namespace std;

class Commande
{
    public:
    Commande();
    virtual ~Commande();
    /*Various geters ans seters*/
    void toString(void);
    void fromStringToInteger();

    private:
    string bytes, from, type, first_s, last_s;
    int value, first_i, last_i;
};

#endif /* COMMANDE_H_ */

Thank you all for your time!

  • If you say you have a problem with `push_back` it would be helpful if you actually *showed* the problematic code (i.e. the code where you call `push_back`). Please try to create a [Minimal, **Complete**, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Jun 09 '16 at 09:11
  • Your edited `main` function should not compile either. `_vct` is a *pointer* to an object so you need to use e.g. `_vct->push_back(...)`. It also doesn't solve the memory leak mentioned in my answer. Lastly, do you come from a Java or C# background? In C++ you don't need to use `new` to create objects, just doing e.g. `Commande cmd;` will create a `Commande` object. – Some programmer dude Jun 09 '16 at 09:15
  • Thank you alll, it is working now. – Guillaume hWS Jun 09 '16 at 09:30

1 Answers1

0

Unrelated to the problem you ask about, but there are many other problems...

For example lets take a closer looks at these lines (excerpt from yout main function):

while(...)
{
    Commande* cmd = new Commande();
    (this)->appendCmdList(*cmd);
}

First of all it should not compile. There's no this in non-member functions, only in non-static member functions.

Then you have the memory leak in that you allocate a new Commande instance dynamically, but you pass the instance (and not the pointer) to the appendCmdList function, which means the next time the loop iterates the pointer is lost (as it goes out of scope) and you allocate a new pointer without deleting the previous object, or saving the pointer.

Without a Minimal, Complete, and Verifiable Example it's impossible to say anything else.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621