0

I have problem only with the push_back function, the compiler said:

CRT detected that the application wrote to memory after end of heap buffer

I want to make a push_back function, that adds a new element to the vector's end.

#pragma once
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstdlib>

class tomb {
private:
    double *adat;
    int szam;
public:
    tomb(){
        adat = NULL;
        szam = 0;
    }
    int meret()const {
        return szam;
    }
    ~tomb() {
        delete[] adat;
    }
    double & operator[](int n) {
        return adat[n];
    }
    const double & operator[](int n)const {
        return adat[n];
    }
    void push_back(const double &a) {
        double *tmp;
        int pos = szam + 1;
        tmp = new double[szam+1];

        for (int i = 0; i < szam; i++)
        {
            tmp[i] = adat[i];
        }

        tmp[pos] = a;
        delete[] adat;
        adat = tmp;
        ++szam;
    }
    void Kiir()const {
        for (int i = 0; i < szam; i++)
        {
            std::cout << adat[i] << "\n";
        }
    }
};
cbuchart
  • 10,847
  • 9
  • 53
  • 93
Zsembes
  • 11
  • 2
  • `tmp[pos] = a` writes to one-past-the-end of your newly allocated block -- `pos` is `szam+1` at that point. – G.M. May 07 '17 at 14:59
  • 1
    By the way, your class violates the Rule Of Three. – aschepler May 07 '17 at 15:00
  • thank you @G.M. for answering :) – Zsembes May 07 '17 at 15:02
  • @aschepler and what does it mean? :D Is that a big problem? – Zsembes May 07 '17 at 15:03
  • At the very least you should use `std::unique_ptr` to manage the lifetime of `adat` instead of doing it yourself. It'll also make it easier to update your code to be exception safe. – Captain Obvlious May 07 '17 at 15:05
  • @Zsembes -- `{ tomb t1; t1.push_back(10.0); tomb t2 = t1;}` -- That code is faulty, even if you fix your `push_back` function. It is faulty due to the violation of the rule of 3. You will be deleting the same pointer value twice on destruction of `t1` and `t2`. – PaulMcKenzie May 07 '17 at 15:07
  • As a side not, you shouldn't pass primitive types (such as double) by const reference, as it might as well be more expensive than a copy. – DeiDei May 07 '17 at 15:10
  • @Zsembes This is where I think it is better to see how a simple vector class is actually implemented instead of making all of the mistakes that can only be corrected by rewriting your entire attempt. – PaulMcKenzie May 07 '17 at 15:10
  • @PaulMcKenzie and how can I correct is? I really never heard about the rule of 3, I'm a beginner – Zsembes May 07 '17 at 15:11
  • @Zsembes -- All of your questions have lengthy answers. So [here is a simple implementation](http://coliru.stacked-crooked.com/a/0b1eaa1bfd7c6c52). – PaulMcKenzie May 07 '17 at 15:17
  • @PaulMcKenzie thank you!!! – Zsembes May 07 '17 at 15:36

2 Answers2

0

pos should be szam not szam+1. You are willing to insert at the last position, which in 0-based indexing is n-1.

cbuchart
  • 10,847
  • 9
  • 53
  • 93
0

The problem is in this line:

tmp[pos] = a;

Since pos is initialized to szam + 1, that is equivalent to:

tmp[szam + 1] = a;

which is one out of the array limit.

The solution is to get rid of pos altogether and just do:

tmp[szam] = a;

BTW, your class is using the default copy constructor and assignment operator, and those will not work properly. You should really do something about that.

rodrigo
  • 94,151
  • 12
  • 143
  • 190