C ++以前分配的数组在类外无法访问

时间:2015-06-02 20:29:07

标签: c++

所以我正在使用C ++中的Table类,到目前为止工作正常,但是从主函数调用[] -operator的类似乎从数组返回错误的值,尽管首先分配它。

第一个调用(参见//第一个注释的行)返回正确的值1,而来自主函数的第二个调用(参见//第二个注释)返回任意值,尽管具有相同的内存地址,导致数据未正确分配的结论(?)。输出如下:

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 with 3 rows and a size of 6 in internal representation!
here at n=1 0059F800,1 
here at 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)
            {
                for (i = 0; i < 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 for <T> = int
                internalArray = tmpArray;                               //
                cout << "Array:[" << internalArray[0];                  //
                for (i = 1; i < sumOfAllRows; i++){                     //
                    cout << "," << internalArray[i];                    //
                };                                                      //
                cout << "]\n";                                          //
        //--------------------------------------------------------------//
                startOfEachRow[0] = &internalArray[0];
                cout << "First element of table: \t" << startOfEachRow[0][0] << "\n";
                for (i = 1; i < 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 with " << numberOfRows_ << " rows and a size of " << sumOfAllRows << " in internal representation!\n";
                int* tmp = new int[];
                tmp = startOfEachRow[0];
                cout << "here at 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 at n=2 " << &tmp[0] << "," << tmp[0] << "\n"; //Second Comment
                return 0;
            }

注意:

我还没完成,因此是testData。

1 个答案:

答案 0 :(得分:1)

下面:

    int tmpArray[] = { 1, 2, 3, 4, 5, 6 }; //TestData for <T> = int
    internalArray = tmpArray;               

使用指向临时数组的指针覆盖以前分配的internalArrayinternalArray = new T[sumOfAllRows];)。

表格c'tor完成后,临时数组的存储“消失” - 它被重新用于其他变量。你很幸运,你的代码没有发生段错/崩溃。