I master s / s ++ to work with CUDA, stuck in arrays. Actually how to transfer an array to a function? there is a code

void print(int ** m); int main() { int b[5][5] ={{ 1,2,3, 4,5},{ 1,2,3, 4,5},{ 1,2,3, 4,5},{ 1,2,3, 4,5},{ 1,2,3, 4,5}}; print((int **)b); return 0; } void print(int **m){ for(int i=0; i<SIZE; i++){ for(int j =0; j<SIZE; j++){ printf(" %d", m[i][j]); } printf("\n"); } } 

It is compiled but crashes constantly when accessing m [i] [j] is attempted. What is the problem?

  • and what is your SIZE? - VladD 1:01 pm
  • #define SIZE 4 is not going beyond the array, as I understand it, but something is near ... - koks_rs


2 answers 2

The question of transferring multidimensional arrays to a function has already been discussed here many times.

You can transfer in two ways: passing either a variable of the type of a multidimensional array, or a simple pointer.

Method 1.

 void print (int m[][5]) { for(int i=0; i<SIZE; i++){ for(int j =0; j<SIZE; j++){ printf(" %d", m[i][j]); } printf("\n"); } } ..... print (b); 

Method 2

 const int SIZE = 5; //Именно 5 !!! void print (int* m) { for(int i=0; i<SIZE; i++){ for(int j =0; j<SIZE; j++){ printf(" %d", m + SIZE*i + j); } printf("\n"); } } ..... print ((int*) b); 

And the pointer to the pointer has nothing to do with it.

If there is something incomprehensible, ask.

And yet: to work in CUDA, very well work on the topics of pointers and addressing members of multidimensional arrays.

  • 2
    Are you sure that the first method is void print (int m[5][]) ? It seemed to me, it is necessary void print (int m[][5]) . - VladD
  • one
    @VladD, you are absolutely right. It is necessary f (int m [] [SIZE]) {... - avp
  • Thank you, you're all right. Corrected. - skegg pm

First of all, the perfect answer is @mikillskegg .

Secondly, here is a conceptually clear solution for you: if you already write in C ++, use C ++ idioms, they will seem more intuitive to you. In particular, use std :: vector instead of native arrays. Your code will look like this:

 #include <iostream> #include <vector> using namespace std; void print(const vector< vector<int> >& v) { for (auto it = v.begin(); it != v.end(); ++it) { for (auto it2 = it->begin(); it2 != it->end(); ++it2) cout << *it2; cout << endl; } } int main() { vector< vector<int> > b(5); for (auto it = b.begin(); it != b.end(); ++it) *it = { 1, 2, 3, 4, 5 }; print(b); return 0; } 

Thirdly, we will analyze the errors. Let me explain what went wrong. int** is a pointer to an array of pointers , right? But int[5][5] is not a pointer array at all! This is a continuous piece of memory to which the compiler knows how to access the index, because it knows the size of each row in your table. Therefore print(b); you did not compile, because the types are different, the arrangement of elements in the memory is not compatible! When you convert it to int** with a hard cast, your pointer to the first element of the table is interpreted as a pointer to a list of pointers, of course, this will not work. Remember: if you need a cast, maybe something goes wrong.

Fourth, if you are still left with native arrays, here's a variation of the @mikillskegg answer closer to the C ++ style (the idea with the template is borrowed from SO):

 template <int LINESIZE> void print (int m[][LINESIZE]) { for(int i=0; i<LINESIZE; i++){ for(int j =0; j<LINESIZE; j++){ printf(" %d", m[i][j]); } printf("\n"); } } 

Fifth, here is another option that may be better. So that the location of the elements in memory is compatible with double pointer, you could manually row the table:

 const int SIZE = 5; void print(int** m) { for(int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) printf(" %d", m[i][j]); printf("\n"); } } // ... int** b = new int*[SIZE]; for(int i = 0; i < SIZE; i++) { b[i] = new int[SIZE]; for (int j = 0; j < size; j++) b[i][j] = j; } print(b); 
  • one
    @VladD, because a person is aiming for CUDA, he needs to work with clean arrays. And before boost :: multi_array it is still far away (by the way, an excellent library for working with multidimensional arrays). - skegg
  • @mikillskegg: you are probably right. Added another option. - VladD