0

I don't get why my code is not working.Without the destructor it's working fine but if I try to delete the array allocated on the heap my Program stops and says

Invalid address specified to RtlValidateHeap

I already googled my Problem but could not find a solution which could help me.

Here is my code:

File : Vektor.h

class Vektor {
private:
    int dimension;
    int* feld;
public:
    Vektor(int dim=0);
    int getAt(int idx); // Getter-Funktion für Werte des Vektors
    // int getDimension(); // Getter-Funktion für Dimension
    Vektor addiereZu(Vektor v); // Addition zweier Vektoren
    Vektor multipliziereMit(int z); // Multiplikation von Vektor mit Zahl
    void ausgeben(); // Ausgabe am Bildschirm (Format ( 0 1 2 ... ) )
        int summiere(); // Vektor-Summation
        void einlesen(); // Werte einlesen
        ~Vektor();
    };

File: Vektor.cpp

#include "Vektor.h"
#include "Funktionen.h"
#include <iostream>
using namespace Funktionen;
using namespace std;

Vektor::Vektor(int dim) {
    feld = NULL;
    // Todo für später:
    if (dim <= 0)
        dim = 0;

    dimension = dim;

    if (dim > 0)
        feld = new int [dimension];

    // Feld mit Werten initialisieren
    for (int i=0; i<dimension; i++)
        feld[i] = 0;
}

int Vektor::getAt(int idx) {
    if (idx >=0 && idx < dimension)
        return feld[idx];
    //else {
        // Fehlerausgabe
        cout << "Vektordimension überschritten.\n";
        // default-Wert zurückgeben
        return 0;
    //}
}

Vektor Vektor::addiereZu(Vektor v) {
    Vektor ergebnis(dimension);
    for (int i = 0; i < dimension; i++) {
        ergebnis.feld[i] = feld[i] + v.feld[i];
    }
    return ergebnis;
}

Vektor Vektor::multipliziereMit(int z) {
    Vektor ergebnis(dimension);
    for (int i = 0; i < dimension; i++) {
        ergebnis.feld[i] = feld[i] * z;
    }
    return ergebnis;
}

void Vektor::ausgeben() {
    // Vektor im Format "( 0 1 2 ... )" ausgeben.
    cout << "( ";
    for (int i = 0; i < dimension; i++) {
        cout << feld[i];
        cout << " ";
    }
    cout << ")";
}

int Vektor::summiere() {
    // Summieren der einzelnen Werte des Vektors
    int summe = 0;
    for (int i = 0; i < dimension; i++) {
        summe += feld[i];
    }
    return summe;
}

void Vektor::einlesen() {
    // Einzelne Werte von der Tastatur einlesen
    for (int i = 0; i < dimension; i++) {
        cout << "Wert für Stelle " << i << ": ";
        // Wert abfragen
        feld[i] = intEingabe(cin);
    }
}

Vektor::~Vektor()
{
    if (dimension > 0)
        delete[] feld; --> Programm stops right here!
}
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37
  • 2
    You included your class, but not a simple `main()` that uses it, and produces the problem. My crystal ball tells me somewhere in the code you chose *not* to post you're passing one of these around *by value*, and in so doing, you may find [What is the Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) an educational read. – WhozCraig Apr 05 '18 at 14:42
  • 1
    this can be helpful https://stackoverflow.com/questions/860447/what-is-the-array-form-of-delete – Nishant Bijani A Apr 05 '18 at 14:44
  • Double check the implementation of your copy constructor. – Eljay Apr 05 '18 at 14:48
  • Welcome to Stack Overflow! Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Apr 05 '18 at 14:52
  • @WhozCraig What exactly do you mean with "passing one of these around with a value"? – Unstopp4ble333 Apr 05 '18 at 17:46
  • Example: `Vektor Vektor::addiereZu(Vektor v)` The `v` in that code is passed in to the `addriereZu` member by *value*. A copy is made. That copy means now you have *two* `Vektor` objects holding a pointers to the *same* data. Think about what happens when those *one* of those objects is destroyed. What does the member pointer of the remaining object now point to? [**Did you *read* the linked article in my prior comment**](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)? A different article, [**like this one**](http://en.cppreference.com/w/cpp/language/rule_of_three) ? – WhozCraig Apr 05 '18 at 18:06

0 Answers0