简体   繁体   中英

passing 2d dynamic array to a function of my class in c++

I have a problem with passing 2d dynamic array to a function of my class.

void s::LoadData(long int &Num_Of_InputDataId,
                 long int **PresentData,
                 long int **InputDataId,
                 long int **InputData)
{
    long int b;

    for (long int i=0;i<Num_Of_InputDataId;i++)
    {
        b = InputDataId[i][0];
        for(long int j=0;j<Num_Of_InputDataId;j++)
        {  
           InputData[i][j]=PresentData[b][j]; //error occur here
        } // end of internal for
    } //end of external for
}

main:

long int Num_Of_InputDataId=10;

long int **PresentData;
PresentData = new long int *[Num_Of_InputDataId];

for (long int ii = 0; ii < Num_Of_InputDataId; ++ii)
    PresentData[ii] = new long int[Num_Of_InputDataId];

long int ** InputDataId;
InputDataId = new long int *[Num_Of_InputDataId];

for (long int ii = 0; ii < Num_Of_InputDataId; ++ii)
    InputDataId[ii] = new long int[2];


long int ** InputData;
InputData = new long int *[Num_Of_InputDataId];

for (long int ii = 0; ii < Num_Of_InputDataId; ++ii)
    InputData[ii] = new long int[Num_Of_InputDataId];

Load.LoadData(Num_Of_InputDataId, PresentData, InputDataId, InputData);

Each of Num_Of_InputDataId, PresentData and InputDataId come from different functions.

For some i , InputDataId[i][0] is greater than or equal to Num_of_InputDataId .

Therefore when you call:

b = InputDataId[i][0];
... = PresentData[b][...];

the array index to PresentData is out-of-bounds, causing a memory error and crash.

As you are not giving the precise error I will make a common sense guess as to the problem. The value of b is an element in your Id array. Given that this is a long int and that your code implies it is some sort of ID code (rather than an index into another array) it is highly likely that the value of it exceeds the index range of your other array PresentData and which could be causing you a memory error from the out of bounds access attempt. You are treating b in code as if it were an index when it seems likely that it is not.

The simplest way to check would be to step through the code using a debugger, (which should automatically be your first attempt at identifying what your problem actually is) rather than guessing from the program error report. In fact using a debugger would probably have taken less time than pasting your code here and asking the question so if you are not using a debugging tool already you really should do so as it will help you no end.

And as the comment from Superman suggests, given that you use C++ take advantage of all the nice things that the STL can do for you and use vectors.

Just as @Superman has commented on your question, here is a simple snippet to make life easy.

#define V(x) std::vector< x>  
typedef V(int)  VI;  
typedef V(VI)  VII; 

void functionDoSomething(VII & arr) {  }

(Passing the vector by reference). By using such typedefs in your code,readability of the code will be better,and you can define nested structures easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM