1

I need your help. I don't understand why but, when I compile my program, it crashes. If I remove the

bool c = tabInt==TabInt2;

in my main, it doesn't crash. Do you have any idea on how to solve the problem?

MonTableau.h

template <class Type> class MonTableau
{

    private:
    int debut;
    int fin;
    int taille;
    Type * adr;

    public:
        MonTableau (int, int);
        MonTableau (int);
        ~MonTableau();
        Type & operator [] (int);           
        bool operator == (MonTableau) const;
        bool  operator != (MonTableau) const;
};

MonTableau.cpp

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



template<class Type> 
MonTableau<Type> :: MonTableau (int d, int f)
    {
        if(f>d)
        {
            debut=d;  fin=f ; taille= f-d; adr= new Type [taille];
        }
        else
        {
            cout << "Taille non valide" << endl;
        }
    }

template<class Type> 
MonTableau<Type>::      MonTableau (int f)
    {
        if(f>0)
        {
            debut=0;  fin=taille=f ; adr= new Type [taille];
        }
        else
        {
            cout << "Taille non valide" << endl;
        }
    }

template<class Type> 
MonTableau<Type>::  ~MonTableau() {delete adr;}

template<class Type> 
    Type& MonTableau<Type>::  operator [] (int i)
    {
    return adr[i-debut];

    }


template<class Type>            
bool MonTableau<Type> :: operator == (MonTableau a) const
{

if(taille==a.taille)
{
    for(int k=0;k<taille;k++)
        {
            if( adr[k] != a.adr[k])  return false;
        }
         return true;
}
else return false;
}




template<class Type> 
bool MonTableau<Type>:: operator != (MonTableau a) const
{
if(taille==a.taille)
{
    for(int i=0;i<taille;i++)
        {
            if(adr[i]!=a.adr[i])  return true;
        }
         return false;
}
else return true;   
}





int main()
{
MonTableau<int> tabInt(5);
MonTableau<int> TabInt2(-2,3);
for(int i=0;i<5;i++)
{
    TabInt2[i-2]=tabInt[i]=i;
}
bool c = tabInt==TabInt2;


return 0;
}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
J.Doe
  • 23
  • 3
  • 4
    Please be careful with your wording. I bet your program compiles fine both ways, but it crashes when you run it. Then: Have you tried to debug it? – Rene Dec 15 '17 at 18:05
  • _@JDoe_ [Notable read for you](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) as you're going to develop this further. – user0042 Dec 15 '17 at 18:07
  • i tried to debug it, but when i press debug, my compilator crashes. What do you mean by ''be careful with your wording"? – J.Doe Dec 15 '17 at 18:08
  • What is the compiler error? – Jake Freeman Dec 15 '17 at 18:08
  • 2
    When you `new []` you must `delete []` (not just `delete`), or the program is undefined. – molbdnilo Dec 15 '17 at 18:11
  • @user0042 Clarified my comment so you can understand it. – molbdnilo Dec 15 '17 at 18:14
  • @molbdnilo I already wondered ;-). It's clear now. – user0042 Dec 15 '17 at 18:15
  • @user0042 i've read it, but there are no exemple of overload operator on templates – J.Doe Dec 15 '17 at 18:17
  • @J.Doe Did you even read what I linked? How does it matter if the functions are operators or not? If you're gonna separate out the `main()` from your implementation `.cpp` file, you'll face linker errors. The template definitions should go to the header file, or a file that can be included to your template header file without scewing up your build system. – user0042 Dec 15 '17 at 18:19
  • but my main is in my .cpp file. I think i don't understand your comment. let me read your link again – J.Doe Dec 15 '17 at 18:23
  • @J.Doe What I mean: It is very rare that a compiler crashes. It can happen, but really only in very rare circumstances. So, I think it is not the compiler that crashes, but your program. Also, when you debug it, I assume you did not set a breakpoint first, so your program is run and crashes. – Rene Dec 15 '17 at 18:26

3 Answers3

1

The problem is that you are not following The Rule of Three.

You are making a shallow copy of the object when you call the operator== function. When that objects gets out of scope, you delete the memory. That memory is deleted again when the variables in main get out of scope. That causes undefined behavior. In your case, that causes the program to crash.

You can temporarily fix the problem by passing const& in the operator== and operator!= functions.

  bool operator == (MonTableau const&) const;
  bool  operator != (MonTableau const&) const;

The real fix is to follow The Rule of Three.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

This line is just wrong:

TabInt2[i-2]=tabInt[i]=i;

You initialize i to 0 but then modify the item at index -2.

Also, you don't initialize the following members, so their value is undefined:

int taille;
Type * adr;
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • 1
    I don't understand why it would be wrong since i've overload the [] operator. Also, my constructor initialise those 2 informations. – J.Doe Dec 15 '17 at 18:19
0

The problem (I suppose) is that your class doesn't have a copy constructor.

So, when you pass MonTableau a as copy to operator!=(), the copy a contain an adr with the same value of the calling TabInt2.

So the memory allocated (new[]) in adr of TabInt2 is destroyed two times: the first one in operator!=(); the second time in main().

This is a typical crash cause.

PS: as observed by molbdnilo, you have to use delete [] for pointers allocated by new []. Also this problem can cause the crash.

max66
  • 65,235
  • 10
  • 71
  • 111