C++ Previously allocated Array not reachable outside of class -


so working on table class in c++ worked fine far , acessing class calling [ ]-operator main function seems return wrong value array despite allocating first.

the first call ( see line //first comment) returns correct value of 1 , while second call main func (see //second comment) returns arbitrary value despite having same memory adress , leading conclusion array(s) not correctly allocated (?) .output listed below:

sumofallrows: 6  array:[1,2,3,4,5,6]  first element of table:      1 first element of n=1-th row: 2  first element of n=2-th row: 4  created table 3 rows , size of 6 in internal representation! here @ n=1 0059f800,1  here @ n=2 0059f800,-858993460 

 template < typename t>     class table{          public:             int i, j;                                                            int numberofrows_;                                                       int sumofallrows = 0;                                                    int* lengthofrows_;                                                      t* internalarray;                        t** startofeachrow;         public:             table(int numberofrows, int lengthofrows[]) :                 numberofrows_(numberofrows), lengthofrows_(lengthofrows)             {                 (i = 0; < numberofrows_; i++){                     sumofallrows += lengthofrows_[i];                 };                 cout << "sumofallrows: " << sumofallrows << "\n" ;                 internalarray = new t[sumofallrows];                 startofeachrow = new t*[numberofrows_];                  int offset = 0;         //--------------------------------------------------------------//                 int tmparray[] = { 1, 2, 3, 4, 5, 6 };                  //testdata <t> = int                 internalarray = tmparray;                               //                 cout << "array:[" << internalarray[0];                  //                 (i = 1; < sumofallrows; i++){                     //                     cout << "," << internalarray[i];                    //                 };                                                      //                 cout << "]\n";                                          //         //--------------------------------------------------------------//                 startofeachrow[0] = &internalarray[0];                 cout << "first element of table: \t" << startofeachrow[0][0] << "\n";                 (i = 1; < numberofrows_; i++){                     offset += lengthofrows_[i - 1];                     startofeachrow[i] = &internalarray[offset];                     cout << "first element of n="<<i<<"-th row:\t" << startofeachrow[i][0] << "\n";                 };                 cout << "created table " << numberofrows_ << " rows , size of " << sumofallrows << " in internal representation!\n";                 int* tmp = new int[];                 tmp = startofeachrow[0];                 cout << "here @ n=1 " << &tmp[0] << "," << tmp[0] << "\n";//first comment             }              size_t rownum() const {                 return numberofrows_;             };             size_t colnum(size_t rowidx) const {                 return lengthofrows_[rowidx];             };              t* operator[](size_t rowidx){                 t* ptr = startofeachrow[rowidx];                 return ptr;             };          };             int main(int argc, char** argv) {                  int lengthofrows[] = { 1, 2, 3 };                 int numberofrows = 3;                 table<int> testtable(numberofrows, lengthofrows);                 int* tmp = testtable[0];                 cout << "here @ n=2 " << &tmp[0] << "," << tmp[0] << "\n"; //second comment                 return 0;             } 

note:

i'm not finished yet , hence testdata .

here:

    int tmparray[] = { 1, 2, 3, 4, 5, 6 }; //testdata <t> = int     internalarray = tmparray;                

you overwrite allocated internalarray (internalarray = new t[sumofallrows];) pointer temporary array.

after table c'tor finishes, temporary array's storage 'vanishes' - re-used other variables. you're lucky code didn't segfault/crash.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -