c++ - Invalid read of size 1 in tetris using valgrind -


i trying write tetris dynamic 2d array of rows , cols named data; make function called edit_2darray duplicate original 2d array , make each column longer, , rest fill space;

==21909== invalid read of size 1 ==21909==    @ 0x100002c0a: tetris::print() const (main.cpp:300) ==21909==    0x1000011cb: test_example() (main.cpp:59) ==21909==    0x100001053: main (main.cpp:36) ==21909==  address 0x10080a9c1 1 bytes inside block of size 2 free'd ==21909==    @ 0x1000132f7: free (in /usr/local/cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so) ==21909==    0x100005188: tetris::edit_2darray(int, int) (tetris.cpp:214) ==21909==    0x1000056ec: tetris::add_piece(char, int, int) (tetris.cpp:279) ==21909==    0x10000118f: test_example() (main.cpp:57) ==21909==    0x100001053: main (main.cpp:36) ==21909==  block alloc'd @ ==21909==    @ 0x100012ebb: malloc (in /usr/local/cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so) ==21909==    0x10005743d: operator new(unsigned long) (in /usr/lib/libc++.1.dylib) ==21909==    0x1000050dd: tetris::edit_2darray(int, int) (tetris.cpp:208) ==21909==    0x1000056ec: tetris::add_piece(char, int, int) (tetris.cpp:279) ==21909==    0x10000118f: test_example() (main.cpp:57) ==21909==    0x100001053: main (main.cpp:36) ==21909== 

here add_piece function size piece , edit_2darray();

private: int width; int* heights; char** data;  tetris::tetris(int awidth) {   width = awidth;   heights = new int[awidth];   (unsigned int = 0; < awidth; i++) {       heights[i] = 0;   }   data = new char* [awidth];   (unsigned int j = 0; j < awidth; j++) {       data[j] = new char[1];       data[j][0] = ' ';   } }  void tetris::add_piece(char c, int m, int n) { int l = 0; int max_h = 0; if (m != 0 &&     m != 90 &&     m != 180 &&     m != 270) {     cerr << "incorrect angle!" << endl; } if (n < 0 ||     n > width-1) {     cerr << "incorrect position" << endl; } if (c == 'i') {     if (m == 0 || m == 180) {         max_h = get_max_height();         heights[n] += 4;         if (heights[n] >= max_h) {             edit_2darray(heights[n], heights[n] - max_h);         }         data[n][heights[n]-4] = c;         data[n][heights[n]-3] = c;         data[n][heights[n]-2] = c;         data[n][heights[n]-1] = c;     }     if (m == 90 || m == 270) {         l = 0;         (unsigned int k = 0; k < 4; k++) {             if (l < heights[n+k]) {                 l = heights[n+k];             }         }         max_h = get_max_height();         heights[n] = l+1;         heights[n+1] = heights[n];         heights[n+2] = heights[n];         heights[n+3] = heights[n];         if (heights[n] >= max_h) {             edit_2darray(heights[n], heights[n] - max_h);         }         data[n][heights[n]-1] = c;         data[n+1][heights[n]-1] = c;         data[n+2][heights[n]-1] = c;         data[n+3][heights[n]-1] = c;         }     } }  void tetris::edit_2darray(int h, int num) { (unsigned int = 0; < width; i++) {     char* temp = new char [h];     (unsigned int j = 0; j < h; j++) {         if (j >= (h-num)) {             temp[j] = ' ';         }         else {             temp[j] = data[i][j];         }     }     delete[] data[i];     data[i] = temp;     delete[] temp;     } }  void test_example() { std::cout << "=====================================================================" << std::endl; std::cout << "test_example()" << std::endl;  tetris tetris(6); std::cout << "empty board width = 6:" << std::endl; tetris.print(); assert (tetris.get_width() == 6);  tetris.add_piece('o',0,1); std::cout << "after adding first piece:" << std::endl; tetris.print(); } 

what valgrind telling in tetris::edit_2darray() allocate memory in line 208 , deallocate in line 214 (i assume char* temp = new char [h]; , delete[] temp; despite line numbers not match). in tetris::print() line 300 (which didn't provide) access freed memory.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -