Tablica dynamiczna, problem z pamięcią.
Ostatnio zmodyfikowano 2016-03-28 19:59
djankooo Temat założony przez niniejszego użytkownika |
Tablica dynamiczna, problem z pamięcią. » 2016-03-27 23:54:53 Witam, staram się stworzyć tablicę dynamiczną. W sumie działa poprawnie, jednak VS15 cały czas wyrzuca mi błąd "Run-Time Check Failure #2 - Stack around the variable 'tab' was corrupted.", którego nie jestem w stanie naprawić. Klasa tablicy : #include <iostream> using namespace std;
class Tablica { private: int size = 1; int * last, * first, * temp; public: Tablica(); void addF( int i ); };
Tablica::Tablica() { first = new int[ size ]; }
void Tablica::addF( int a ) { temp = new int[ ++size ]; memcpy( temp, first,( size - 1 ) * sizeof( int ) ); free( first ); first = temp; last = first + size - 2; * last = a; }
void Tablica::read() { int * w = first; for( int i = 0; i < size - 1; i++ ) { cout << * w << endl; w++; } }
Klasa z mainem : #include <iostream> #include <time.h> #include <cstdlib> #include "Tablica.h" using namespace std;
int main() { clock_t start, stop; double time; Tablica tab = Tablica(); start = clock(); for( int i = 0; i < 15; i++ ) { tab.addF( 5 ); } stop = clock(); time =( double )( stop - start ) / CLOCKS_PER_SEC; tab.read(); cout << endl << "Time: " << time << endl; system( "pause" ); return 0; }
Ponadto występuje problem z wypisywaniem (funkcja read) "Exception thrown: read access violation. w was nullptr. If there is a handler for this exception, the program may be safely continued.", ale tylko wtedy kiedy próbuje mierzyć czas. Gdy zakomentuję "time = (double)(stop - start)/CLOCKS_PER_SEC; " problem znika. z góry dziękuję za pomoc, pozdrawiam, Jan |
|
carlosmay |
» 2016-03-28 01:48:55 Brakuje deklaracji metody read w klasie, poza tym kod chyba jest OK.
Można by jeszcze destruktor dorzucić. |
|
djankooo Temat założony przez niniejszego użytkownika |
» 2016-03-28 17:03:22 Poprawiłem trochę kod i dopisałem kilka innych funkcji. Czy mógłby ktoś rzucić okiem i stwierdzić czy kod jest poprawny? #include <iostream> #include <time.h> using namespace std;
class Tablica { private: int size = 1; int * last, * first; public: Tablica(); void addToFront( int i ); void addToEnd( int i ); void read(); void deleteFromFront(); void deleteFromEnd(); void addToRand( int a ); int sizeOfTab(); void deleteFromRand(); void searchigFor( int a ); };
Tablica::Tablica() { srand( time( NULL ) ); first = new int[ size ]; last = first; }
int Tablica::sizeOfTab() { return size; }
void Tablica::addToEnd( int a ) { * last = a; int * temp = new int[ ++size ]; memcpy( temp, first,( size - 1 ) * sizeof( int ) ); delete[] first; first = temp; last = first + size - 1; }
void Tablica::addToFront( int a ) { int * temp = new int[ ++size ]; memcpy( temp + 1, first,( size - 1 ) * sizeof( int ) ); delete[] first; first = temp; last = first + size - 1; * first = a; }
void Tablica::deleteFromEnd() { if( size != 0 ) { int * temp = new int[ --size ]; memcpy( temp, first, size * sizeof( int ) ); delete[] first; first = temp; last = first + size - 1; } else { cout << "Brak elementow do odejmowania." << endl; } }
void Tablica::deleteFromFront() { if( size != 0 ) { int * temp = new int[ --size ]; memcpy( temp, first + 1, size * sizeof( int ) ); delete[] first; last = first + size - 1; first = temp; } else { cout << "Brak elementów do odejmowania." << endl; } }
void Tablica::addToRand( int a ) { if( size > 1 ) { int random =( std::rand() % size ) + 0; int * temp = new int[ ++size ]; memcpy( temp, first, random * sizeof( int ) ); int * w = temp + random; * w = a; memcpy( temp + random + 1, first + random,( size - 1 - random ) * sizeof( int ) ); delete[] first; first = temp; last = first + size - 1; } else { int * temp = new int[ ++size ]; memcpy( temp + 1, first,( size - 1 ) * sizeof( int ) ); delete[] first; first = temp; last = first + size - 1; * first = a; } }
void Tablica::deleteFromRand() { if( size != 0 ) { int random =( std::rand() % size ) + 0; int * temp = new int[ --size ]; memcpy( temp, first, random * sizeof( int ) ); memcpy( temp + random, first + random + 1,( size - random ) * sizeof( int ) ); first = temp; last = first + size - 1; } else { cout << "Brak elementow do odejmowania." << endl; } }
void Tablica::searchigFor( int a ) { int * w = first; for( int i = 0; i < size; i++ ) { if( * w == a ) { cout << w; } } }
void Tablica::read() { int * w = first; for( int i = 0; i < size; i++ ) { cout << * w << endl; w++; } }
|
|
carlosmay |
» 2016-03-28 18:26:15 Można by jeszcze destruktor dorzucić. |
Przydałoby się, aby destruktor zwalniał pamięć. |
|
mokrowski |
» 2016-03-28 19:59:20 Oprócz dodania destruktora, inicjuj atrybuty w liście inicjalizacyjnej a nie w ciele konstruktora. |
|
« 1 » |