Witam
Mam małe pytanie do was. Otóż w ramach ćwiczeń z C++ postanowiłem sobie napisać szablon klasy tablicy dynamicznej. i napisałem sobie coś takiego:
#include <iostream>
#include <conio.h>
typedef unsigned int Uint;
template<class type>
class Array
{
private:
type * Data;
Uint Size;
public:
//konstruktor
Array();
Array(Uint HowIndex);
//destruktor
~Array();
//metody zwracajace
Uint Length(); //zwraca ilosc elementow tablicy
Uint High(); //zwraca index ostatniego elementu
Uint GetFirstEmptyIndex();
Uint GetLastEmptyIndex();
//operator
type& operator[](const Uint Index)
{
return Data[Index];
};
//metody robiace cos
void Clear();
};
template<typename type>
Array<type>::Array()
{
Size = 1;
type * Data = new type[Size];
};
template<typename type>
Array<type>::Array(Uint HowIndex)
{
Size = HowIndex;
type * Data = new type[Size];
};
template<typename type>
Array<type>::~Array()
{
delete [] Data;
};
template<typename type>
void Array<type>::Clear()
{
delete [] Data;
};
template<typename type>
Uint Array<type>::Length()
{
return Size;
};
template<typename type>
Uint Array<type>::High()
{
return (Size-1);
};
template<typename type>
Uint Array<type>::GetFirstEmptyIndex()
{
bool LoopOff = false;
Uint I;
while (LoopOff = true)
{
if(Data[I] = 0)
{
return I;
LoopOff = true;
} else I++;
};
};
template<typename type>
Uint Array<type>::GetLastEmptyIndex()
{
bool LoopOff = false;
Uint I = High();
while (LoopOff = true)
{
if(Data[I] = 0)
{
return I;
LoopOff = true;
} else I--;
};
};
typedef Array<int> Int_Array;
typedef Array<float> Flt_Array;
typedef Array<bool> Bl_Array;
int main()
{
Int_Array a(350000);
Flt_Array b(400000);
Bl_Array c(500000);
getch();
a.Clear();
b.Clear();
c.Clear();
getch();
};
I tu rodzi się pytanie. Dlaczego VC++ Express 2010 wywala mi wyjątek w deallokacji pamięci metodą Clear() o treści:
Unhandled exception at 0x59f059da (msvcr100d.dll) in resoruces test.exe: 0xC0000005: Access violation reading location 0xccccccc0.
a Dev-C++ nie, tylko nie dealokuje pamięci poprawnie i powstaje wyciek?