-1

I am a total new to C++. Can anybody explain me why it gives a Heap Exception with the following Code. I am trying to create an array from Type Student with different Student objects. The Code compiles sometimes successfully, and sometimes it returns -1073741510 with a longer time.

#include <iostream>
using namespace std;

class Student{
private:
    const char* vn;
    const char* nn;
    int mn;
    int fs;
public:
    Student() = default;
    Student(const char* vn, const char* nn, int mn, int fs): vn(vn), nn(nn), mn(mn), fs(fs){}

    const char* getVn() {
        return vn;
    }
    const char* getNn() {
        return nn;
    }
    int getMnr() {
        return mn;
    }
    int getFs() {
        return fs;
    }
};

class Verwaltung{
private:
    unsigned counter = 0;
    Student *s = new Student[counter];
public:
    void add(Student &student){
        s[counter]=student;
        cout << counter << " " << s[counter].getNn() << " " << s[counter].getVn() << " " << s[counter].getMnr() << " " << s[counter].getFs() << endl;
        ++counter;
    }
};

int main(){
    Student s1("Micha", "Nugel", 3213, 8);
    Student s2("Mohan", "Sasa", 32211, 3);
    Student s3("Johan", "Goat", 3213, 2);
    Student s4("Meise", "Kreise", 3123, 1);
    Verwaltung v;
    v.add(s1);
    v.add(s2);
    v.add(s3);
    v.add(s4);
} 
trincot
  • 317,000
  • 35
  • 244
  • 286
Johayn
  • 1
  • When you create `v`, how many `Students` can it hold? Also have you run the code through a debugger to know when it crashes? – cigien Jun 06 '20 at 20:21
  • 1
    you can use `std::vector` for dynamic arrays – 463035818_is_not_an_ai Jun 06 '20 at 20:23
  • 2
    "The Code compiles sometimes succeful" dont confuse compiling and running the resulting executable. Compiling either fails or not, but usually not "sometimes" – 463035818_is_not_an_ai Jun 06 '20 at 20:24
  • @cigien It didnt crash. it doesn't crash. it just doesn't return 0. – Johayn Jun 06 '20 at 20:28
  • I'm not sure what you mean. How are you compiling the code and running it? – cigien Jun 06 '20 at 20:32
  • @idclev463035818 i would like to try an array instead of a vector. if the program does not return 0, something must be wrong – Johayn Jun 06 '20 at 20:33
  • 1
    _i would like to try an array instead of a vector_ Why? Why make life harder for yourself? Professional programmers don't do that. – Paul Sanders Jun 06 '20 at 20:42
  • dymanic arrays with pointers is like juggling with sciccors. Seriously, I wouldnt get it right myself. You should never use raw owning pointers and less so for arrays, use smart pointers instead. Even if you fix the current problem your `Student` is serisouly broken. You ignore the rule of 3/5. It can be an exercise, but to make that worthwhile you need to know first what is needed instead of just trying something (no offense but thats what your code looks like). The best I can recommend you is to pick a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 463035818_is_not_an_ai Jun 06 '20 at 20:57
  • long story short: You learn something much more useful if you take a look at `std::vector` – 463035818_is_not_an_ai Jun 06 '20 at 20:58
  • I find arrays very trying (compared to vectors) ... but if you want to try, I suggest you start with something somewhat easier. Consider a fixed array size ... perhaps 4, or maybe 8 if you want to provide some room before re-allocating a bigger array. So, the first allocation should be 4 Students, and you place the 1st four Verwaltung::add()'s using index 0..3. When you test that, and if it seems to work, then try allocating a new array doubling the space (then moving the original element,s deleting the original array, etc.). (FYI 0 some std::vectors double the space during growth). – 2785528 Jun 06 '20 at 22:32
  • Have you looked up (or coded a test) to see how many elements the array will have when allocated by "s = new Student [0];" ? – 2785528 Jun 06 '20 at 22:37

1 Answers1

0

Well, have a look to this 2 lines of code:

class Verwaltung{
    unsigned counter = 0;
    Student *s = new Student[counter];
}

So your s pointer will point to a 0-sized array, and so you are going to assign all the students to memory is not been allocated, and if that memory doesn't belongs to your program, or has something else on it, you will corrupt memory, and so your program will fails.

If you want all of those checking done automatically, use std::vector

Infact, error -1073741510 corresponds to 0xc000013a which is an error to signal that the program is beed stopped anomalously (isn this case probably by the SO in order to prevent memory corruption or for safety reasons, like "not reading others programs memory")

Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48