0

I have a simple c++ phonebook project, and I'm no allowed to use std::vector. So i decided i create one for myself. And I'd like to ask a little help about my push_back function.

So this is in the vector.h:

class Vector{
    int siz;            //size
    std::string* elem;  //elements
    public:
    Vector(): siz(0){}

    int getSize()const;
    void pushBack(std::string const&);

    std::string operator[](int) const;
    Vector& operator=(const Vector&);
};

And this is my push_back function:

void Vector::pushBack(std::string const& s){
std::string* temp = new std::string[siz + 1];
for(int i = 0; i < siz; i++)
    temp[i] = elem[i];
temp[siz] = s;

//    delete[] elem;              // The debugger points here
this->elem = temp;
this->siz += 1;
}

This is where I use the push_back function:

const Vector read(){
    Vector v;
    std::ifstream file;
    file.open("data.txt", std::ios::in);
    int db = 10 * linecounter();
    std::string temp;
    for(int i = 0; i < db; i++)
    {
        if(i % 10 == 9 && i != 0){
            getline(file, temp);          // the last data in the line after that ther is a \n
            v.pushBack(temp);
        }
        else{
            getline(file, temp, ';');     // read in the temp till the ;
            v.pushBack(temp);             // The debugger points here
        }
    }
    return v;
}

And my problen is, when I dont't use delete[] elem; It's going to create some memory leak. When I use, the program crashes immediately.

DeiDei
  • 10,205
  • 6
  • 55
  • 80

2 Answers2

3

A raw pointer that is not initalized might hold a random memory address.

So if you call delete[] elem; in you first pushBack, then you try to delete a random memory address:

delete expression:

For delete [] form, expression must be a null pointer value or a pointer value previously obtained by an array form of new-expression. If expression is anything else, including if it's a pointer obtained by the non-array form of new-expression, the behavior is undefined.

To solve this you need to initalize std::string* elem in your construtor:

Vector(): siz(0), elm(nullptr) {}
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • Thank for your tips! I did initialize elem to NULL, but I still get a memory leak. My leaker tester points to the std::string* temp = new std::sting[siz+1] line. It says new[] dosn't have a proper delete[] – Ádám Szűcs May 12 '18 at 13:52
  • 1
    Do you delete elem in the destructor of Vector? But remember to follow the rule of three (or five) and also define the copy constructor etc – Ian4264 May 12 '18 at 17:42
-3

seems you access the "elem" after deleting it. do the delete just at the end of the function

do you create what you are deleting with "new"?

also: as a optimization, think of increasing the size of your vector in bigger chunks to safe the effort of reallocating on every push-back

skeller
  • 1,151
  • 6
  • 6
  • 2
    The problem is not that `elem` is accessed after it is deleted, but for the first `pushBack` the `delete[] elem;` would be called on a random memory address because `elem` is not initalized to e.g. `nullptr`. – t.niese May 12 '18 at 13:09