0

I am trying to access values of a 2D array initially passed as an array of pointers in the function. Additionally I would like this function to return a 2D array with the values I have accessed and worked on.

I understand the concept of pointer, however I have trouble with the double pointer concept of 2D arrays. If I understand correctly, iterating over the first dimension of the array points to a position in the memory of the computer, which itself points to another position of the memory in the computer where the value is. This 2 pointing process represents the 2D.

Now, in my code, I don't manage to access the values of this second pointer and I don't understand why.

extern "C" double cfun(double **indatav, int rows, int cols)
{

    double* *outdatav = new double*[rows];
    for (int i=0; i < rows; i++){
        outdatav[i] = new double*[cols];
        }

    for (int i=0; i < rows; i++){
        for (int j=0; j < cols; j++){
            outdatav[i][j] = indatav[i][j];
            }
        }
    return outdatav;
}

The code above is called through ctypes in Python and therefore it has this extern... However the equivalent in C++ would just consist in removing this extern "C" I have spent quite some time now on this problem and I don't think I will be able to solve it alone so any help would be highly appreciated.

2
  • *indata[i][j] is not valid. intdatav[i] IS a pointer and points to the start of row I. Just use outdatav[i][j] = indatav[i][j]; Commented Sep 27, 2019 at 19:25
  • Note that you'll also have to free all those pointers. If you're going to access this func from Python only I thought about a workaround that would not require Python code to call the deallocation func. I was preparing it for one of the 2 question you deleted. Commented Sep 30, 2019 at 10:16

2 Answers 2

1

seems wrong memory allocation in double pointer

double* *outdatav = new double*[rows];
for (int i=0; i < rows; i++){
    // outdatav[i] = new double*[cols]; // <- typo here
    outfatav[i] = new double [cols]; // <- replace with this
}

ADD

the return type of the function is not matched with the returned variable, so need to revise the return type to double**

extern "C" double** cfun(double **indatav, int rows, int cols)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response, but it still doesn't entirely solve my problem (see edit)
for *indatav[i][j];, already indatav[i][j] points to the value (type as double), not an address
1

extern "C" double cfun(double **indatav, int rows, int cols)

{

    double**outdatav = new double*[rows];
    for (int i=0; i < rows; i++){
        outdatav[i] = new double[cols];
        }

    for (int i=0; i < rows; i++){
        for (int j=0; j < cols; j++){
            outdatav[i][j] = indatav[i][j];
            }
        }
    return outdatav;
}

This will work properly now.

 outdatav[i] = new double[cols];

In this line of code, you were assigning an array of pointers to a pointer. Instead, you have to assign array to each pointer.

4 Comments

Thank you very much for your response and edit, but it still doesn't entirely solve my problem because I want outdatav to be a matrix of values, and not pointers to pointers.
no problem at all :).. The above code solved your problem right?
Well the code now works correctly according to your recommendations, which was the question of this thread. However, I am not able to do what I want but I am not sure where does the problem come from, if it's from Python or from C++, so therefore I did another thread on the subject where I am detailing more precisely what exactly my problem is. stackoverflow.com/questions/58150201/…
@JoachimBsh: Note: It takes an user to have 10k+ reputation in order to be able to view deleted posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.