0

I have a function:

void ord_matrix_multiplication(Cache& cache, Block* block1[][], Block* block2[][], Block* block3[][], int A[][], int B[][], int C[][], int i, int j, int k, int n, int s)

i have following code in the calling function:

int A[n][n];                                        
Block* blocks1[n][n];                               
int B[n][n];                                        
Block* blocks2[n][n];                               
int C[n][n];                                      
Block* blocks3[n][n]; 
...
//some code
...
ord_matrix_multiplication(cache, blocks1, blocks2, blocks3, A, B, C, i, j, k, n, s);

But i'm getting following error:

cacheaware.cpp:35: error: declaration of ‘block1’ as multidimensional array must have  bounds for all dimensions except the first
cacheaware.cpp:35: error: expected ‘)’ before ‘,’ token
cacheaware.cpp:35: error: expected initializer before ‘*’ token

I then changed the function dclaration to:

void ord_matrix_multiplication(Cache& cache, Block* block1[][100], Block* block2[][100], Block* block3[][100], int A[][100], int B[][100], int C[][100], int i, int j, int k, int n, int s)

On doing so, i'm getting:

cannot convert ‘Block* (*)[(((unsigned int)(((int)n) + -0x00000000000000001)) + 1)]’ to ‘Block* (*)[100]’ for argument ‘2’ to ‘void ord_matrix_multiplication(Cache&, Block* (*)[100], Block* (*)[100], Block* (*)[100], int (*)[100], int (*)[100], int (*)[100], int, int, int, int, int)’

Could someone please tell me how i could fix this?

6
  • 3
    Unless n is a constant, you're using variable length arrays which are not standard in C++. Why not use std::vector instead? You can make a vector of vectors, and pass it around easier. Commented Jul 2, 2013 at 7:37
  • @JoachimPileborg : n is passed as an argument to the calling function. Commented Jul 2, 2013 at 7:42
  • @JoachimPileborg : Is there any way other than using vectors? Commented Jul 2, 2013 at 7:43
  • Since you use VLAs, not really. Commented Jul 2, 2013 at 7:53
  • 1
    Why are you passing data around? Encapsulate it in a class, and perform operations on it. Commented Jul 2, 2013 at 8:14

2 Answers 2

1

Multidimensional arrays are not managed in C++, you must declare your function as:

void ord_matrix_multiplication(Cache& cache, Block* block1, Block* block2, Block* block3, int* A, int* B, int* C, int i, int j, int k, int n, int s)

Thereafter you may index into those arrays in a multidimensional fashion, although you are responsible for data integrity and bound checking.

Additionally, it is not standard C++ to be able to declare arrays of variable sizes. The sise of the array must be known at the time of declaration, or allocated in an initialization function using the 'new' keyword.

Sign up to request clarification or add additional context in comments.

Comments

0

I faced problems like that before. the easiest solution is using pointers

int* x; // x can be pointer to int or pointer to the first item in the array
int** y; // y can be a pointer to pointer of int or a pointer to array of int or 2 dimension array
int x*** z; // z can be used as three dimensional array

also you can make your function take pointer to any type void* and cast it to the type that you are sure you will pass it

2 Comments

While it's true that a variable declared as int** can be used as a two dimensional array, it is not a two-dimensional array. The layout of a proper two dimensional array and a pointer-to-pointer array are completely different, which means you can't do it like that.
@Joachim Pileborg : ok as you said can be used as 2 dimensional array, it needs more precautions to free the allocated memory this is the responsibility of the developer, I'm trying to give a solution

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.