Multi dimensional array as VLA function parameter with explicit size - arrays

Context
I am working on a computational fluid dynamics code where I work with vector and matrices. Consequently I want to pass them to functions every once in a while.
The memory for vectors and matrices is allocated as follows consequently throughout the code
double* vec = calloc(n, sizeof(double));
double* mat = calloc(n * m, sizeof(double));
meaning I would like to access matrices like this mat[i * m + j].
In order to "auto-document" functions I would like to include the size of these arrays into the function prototype. For vectors this works flawlessly and looks something like this
// vecmat.h
void vec_print(size_t n, double const vec[restrict n]);
// vecmat.c
void vec_print(size_t n, double const vec[restrict n])
{
for (size_t i = 0; i < n; ++i) {
printf(DBL_FMT, vec[i]);
}
putchar('\n');
}
Problem
To keep things consistent, I would like to do the same thing with matrices, so I did this
// vecmat.h
void mat_print(size_t n, size_t m, double const mat[restrict n * m]);
// vecmat.c
void mat_print(size_t n, size_t m, double const mat[restrict n * m])
{
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
printf(DBL_FMT, mat[i * m + j]);
}
putchar('\n');
}
putchar('\n');
}
Here comes the issue though. When I compile, I get the following warning
src/vecmat.c:21:49: warning: argument 3 of type ‘const double[restrict n * m]’ declared with mismatched bound ‘n * m’ [-Wvla-parameter]
21 | void mat_print(size_t n, size_t m, double const mat[restrict n * m])
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from src/vecmat.c:1:
src/vecmat.h:16:49: note: previously declared as ‘const double[restrict n * m]’ with bound ‘n * m’
16 | void mat_print(size_t n, size_t m, double const mat[restrict n * m]);
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
The code compiles fine and also produces the expected output, but I would like to get rid of the warning message. I know that C does not use the provided dimension n * m of the array, but rather silently converts the argument into double* restrict mat. The problem seems to have something to do with the fact that I am using a multiplication and gcc somehow thinks that the array bounds are inconsistent.
Question
Can I somehow modify my syntax to avoid this compiler warning, or can I somehow disable this warning in gcc altogether? (gcc does not have the flag -Wno-vla-parameter)
EDIT -Wno-vla-parameter does exist and removes the warnings
Solution
Matrices should be allocated as follows:
double (*mat)[m] = calloc(n * sizeof(*mat));
The function calls with matrices in them should be altered as follows:
void mat_print(size_t n, size_t m, double const mat[restrict n][m]);
This makes it possible to access the matrix elements in a simpler form mat[i][j].
Unfortunately gcc still complains, because the function uses const. (https://godbolt.org/z/6xh5Exsff) If this also has a clever fix my initial and all subsequent problems would be solved :D
Additional resources:
http://c-faq.com/aryptr/dynmuldimary.html
http://c-faq.com/aryptr/ary2dfunc3.html
Additional Info
gcc version 11.1.0
-std=c99

Related

Matrix multiplication in C with unknown sizes

As a C newbie I am struggeling with matrix multiplication in C. The problem is, that the multiplication should be flexible and the rows, cols are not known before.
The dimensions, matrices and results for different matrix multiplications are all defined in an header file however I would like to have an matrix multiplication function that works for all of them.
Up to now I have:
void matrix_multiply(int rows1, int cols1, int cols2, float matrix1[rows1][cols1], const float matrix2[cols1][cols2], float result[rows1][cols2])
{
for (int i = 0; i < rows1; i++)
{
for (int j = 0; j < cols2; j++)
{
result[i][j] = 0;
for (int k = 0; k < cols1; k++)
{
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
My local compiler accepts that. However when I try with other compilers at godbolt the compiler may return an error. It seems to compile with gcc and clang however with others I get errors:
x86 msvc v19.27:
<source>(2): error C2057: expected constant expression
<source>(2): error C2466: cannot allocate an array of constant size 0
<source>(2): error C2087: 'matrix1': missing subscript
<source>(2): error C2087: 'matrix2': missing subscript
<source>(2): error C2087: 'result': missing subscript
Is there a way to programm a matrix multiplication function that works for every compiler?
I don't know why your code works, it should fail on every C compiler.
There can be at most one variable dimension size.
You have two option:
Change 2d-array into array of arrays:
void matrix_multiply(
int rows1, int cols1, int cols2,
const float *matrix1[],
const float *matrix2[],
float *result[])
Or, just use 1-dimensional row-major array. For example:
// 2-dimensional array
int x[ROWS][COLS];
// can be replaced by
int y[ROWS*COLS];
// where element
x[r][c];
// corresponds to
y[r*COLS+c];

How to convert from double pointer to gsl vector?

So i created this function to convert from double pointer to gsl vector :
void convert_dpvect_gslvect(int N, double *x, gsl_vector *gx)
{
gx->size = N;
for (int i = 0; i < N; i++) {
gx->data[i] = x[i];
}
}
does that make sense? i want to make sure that it coverts correctly. I would really appreciate your help with this.
By looking at the online documentation for gsl lib (link) we can find functions that already do what you want to do. As a general rule, whenever using a type defined in a library you should look for the provided functions to handle such type.
The rationale behind this is that they may take care of errors or other fields that you might forget while implementing your functions.
In your specific case, it seems that you have a vector of double and you want to assign each element to the elements of a gsl_vector. I say this because you do:
gx->data[i] = x[i]; // Access up to N elements from x
What we want is then the provided library function
void gsl_vector_set(gsl_vector *v, const size_t i, double x)
This function sets the value of the i-th element of a vector v to x. If i lies outside the allowed range of 0 to size - 1 then the error handler is invoked. An inline version of this function is used when HAVE_INLINE is defined.
In order to use it we need to be sure that we have allocated enough memory by creating a gsl_vector before. I assume that you have N elements, so the full code would be:
// assume x is already initialized with N elements
gsl_vector* V = gsl_vector_alloc(N);
for (int i = 0; i < N; i++) {
gsl_vector_set( V , i, x[i] );
}
Interestingly enough, by looking at the source code of gsl_vector_set it does something similar to what you came up with, but of course there are some nuisance that are crucial to the library, like checking the range and using the stride to account for different block sizes.
// from https://github.com/ampl/gsl/blob/master/vector/gsl_vector_double
INLINE_FUN
void
gsl_vector_set (gsl_vector * v, const size_t i, double x)
{
#if GSL_RANGE_CHECK
if (GSL_RANGE_COND(i >= v->size))
{
GSL_ERROR_VOID ("index out of range", GSL_EINVAL);
}
#endif
v->data[i * v->stride] = x;
}

Passing a 2D array to a function in C?

I need to pass a 2D array to a function.
#include <stdio.h>
#define DIMENSION1 (2)
#define DIMENSION2 (3)
void func(float *name[])
{
for( int i=0;i<DIMENSION1;i++){
for( int j=0;j<DIMENSION2;j++){
float element = name[i][j];
printf("name[%d][%d] = %.1f \n", i, j, element);
}
}
}
int main(int argc, char *argv[])
{
float input_array[DIMENSION1][DIMENSION2] =
{
{0.0f, 0.1f, 0.2f},
{1.0f, 1.1f, 1.2f}
};
func(input_array);
return 0;
}
Dimensions vary depending on the use case, and the func should stay the same.
I tried the above int func(float *[]) but compiler complains expected ‘float **’ but argument is of type ‘float (*)[3]’, and also I get the segmentation fault error at runtime when trying to access the array at element = name[i][j].
What would be the proper signature of my function? Or do I need to call the func differently?
You can use the following function prototype:
int func(int dim1, int dim2, float array[dim1][dim2]);
For this you have to pass both dimensions to the function (you need this values anyhow in the function). In your case it can be called with
func(DIMENSION1, DIMENSION2, input_array);
To improve the usability of the function call, you can use the following macro:
#define FUNC_CALL_WITH_ARRAY(array) func(sizeof(array)/sizeof(*(array)), sizeof(*(array))/sizeof(**(array)), array)
Then you can call the function and it will determine the dimensions itself:
FUNC_CALL_WITH_ARRAY(input_array);
Full example:
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define FUNC_CALL_WITH_ARRAY(array) func(sizeof(array)/sizeof(*(array)), sizeof(*(array))/sizeof(**(array)), array)
int func(int dim1, int dim2, float array[dim1][dim2])
{
printf("dim1 %d, dim2 %d\n", dim1, dim2);
return 0;
}
#define DIMENSION1 (4)
#define DIMENSION2 (512)
int main(int argc, char *argv[])
{
float input_array[DIMENSION1][DIMENSION2];
FUNC_CALL_WITH_ARRAY(input_array);
float input_array2[7][16];
FUNC_CALL_WITH_ARRAY(input_array2);
}
Will print
dim1 4, dim2 512
dim1 7, dim2 16
Dimensions vary depending on the use case, and the func should stay the same.
Use VLA:
void func (int r, int c, float arr[r][c]) {
//access it like this
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
printf ("%f\n", arr[i][j]);
}
}
}
// call it like this
func (DIMENSION1, DIMENSION2, input_array);
You can change your function like this;
int func(float (*arr)[DIMENSION2])
{
}
But also you should change your main code like this;
float input[DIMENSION1][DIMENSION2];//I just upload the dimension1 to dimension2
As noted above in the comment, the key problem is that int func(float *name[]) declares name to be an array of pointers to float.
In this sense, the following modification to main() works:
int main(int argc, char *argv[])
{
float input_array[DIMENSION1][DIMENSION2] =
{
{0.0f, 0.1f, 0.2f},
{1.0f, 1.1f, 1.2f}
};
/* Declare an array of pointers, as this is what func requires at input: */
float* in_p[DIMENSION1];
/* ... and initialize this array to point to first elements of input array: */
for( int i=0;i<DIMENSION1;i++)
in_p[i] = input_array[i];
/* ... and send this array of pointers to func: */
func(in_p);
return 0;
}
This is going to present a very old solution, one that works on every C compiler that exists. The idea goes something like this:
I have multiple pieces of information to keep track of
I should keep them together
This leads us to the idea that we can use a composite type to hold all the related information in one place and then treat that object as a single entity in our code.
There is one more pebble in our bowl of sand:
the size of the information varies
Whenever we have varying-sized objects, dynamic memory tends to get involved.
Arrays vs Pointers
C has a way of losing information when you pass an array around. For example, if you declare a function like:
void f( int a[] )
it means exactly the same thing as:
void f( int * a )
C does not care that the size of the array is lost. You now have a pointer. So what do we do? We pass the size of the array also:
void f( int * a, size_t n )
C99 says “I can make this prettier, and keep the array size information, not just decay to a pointer”. Okay then:
void f( size_t dim1, size_t dim2, float array[dim1][dim2] )
We can see that it is pretty, but we still have to pass around the array’s dimensions!
This is reasonable, as the compiler needs to make the function work for any array, and array size information is kept by the compiler, never by executable code.
The other answers here either ignore this point or (helpfully?) suggest you play around with macros — macros that only work on an array object, not a pointer.
This is not an inherently bad thing, but it is a tricky gotcha: you can hide the fact that you are still individually handling multiple pieces of information about a single object,
except now you have to remember whether or not that information is available in the current context.
I consider this more grievous than doing all the hard stuff once, in one spot.
Instead of trying to juggle all that, we will instead use dynamic memory (we are messing with dynamic-size arrays anyway, right?)
to create an object that we can pass around just like we would with any other array.
The old solution presented here is called “the C struct hack”. It is improved in C99 and called “the flexible array member”.
The C struct hack has always worked with all known compilers just fine, even though it is technically undefined behavior.
The UB problem comes in two parts:
writing past the end of any array is unchecked, and therefore dangerous, because the compiler cannot guarantee you aren’t doing something stupid outside of its control
potential memory alignment issues
Neither of these are an actual issue. The ‘hack’ has existed since the beginning (much to Richie’s reported chagrin, IIRC), and is now codified (and renamed) in C99.
How does this magic work, you ask?
Wrap it all up in a struct:
struct array2D
{
int rows, columns;
float values[]; // <-- this is the C99 "flexible array member"
};
typedef struct array2D array2D;
This struct is designed to be dynamically-allocated with the required size. The more memory we allocate, the larger the values member array is.
Let’s write a function to allocate and initialize it properly:
array2D * create2D( int rows, int columns )
{
array2D * result = calloc( sizeof(array2D) + sizeof(float) * rows * columns, 1 ); // The one and only hard part
if (result)
{
result->rows = rows;
result->columns = columns;
}
return result;
}
Now we can create a dynamic array object, one that knows its own size, to pass around:
array2D * myarray = create2D( 3, 4 );
printf( "my array has %d rows and %d columns.\n", myarray->rows, myarray->columns );
free( myarray ); // don’t forget to clean up when we’re done with it
The only thing left is the ability to access the array as if it were two-dimensional.
The following function returns a pointer to the desired element:
float * index2D( array2D * a, int row, int column )
{
return a->values + row * a->columns + column; // == &(a->values[row][column])
}
Using it is easy, if not quite as pretty as the standard array notation.
But we are messing with a compound object here, not a simple array, and it goes with the territory.
*index2D( myarray, 1, 3 ) = M_PI; // == myarray[ 1 ][ 3 ] = M_PI
If you find that intolerable, you can use the suggested variation:
float * getRow2D( array2D * a, int row )
{
return a->values + row * a->columns; // == a->values[row]
}
This will get you “a row”, which you can array-index with the usual syntax:
getRow2D( myarray, 1 )[ 3 ] = M_PI; // == myarray[ 1 ][ 3 ] = M_PI
You can use either if you wish to pass a row of your array to a function expecting only a 1D array of floats:
void some_function( float * xs, int n );
some_function( index2D( myarray, 1, 0 ), myarray->columns );
some_function( getRow2D( myarray, 1 ), myarray->columns );
At this point you have already seen how easy it is to pass our dynamic 2D array type around:
void make_identity_matrix( array2D * M )
{
for (int row = 0; row < M->rows; row += 1)
for (int col = 0; col < M->columns; col += 1)
{
if (row == col)
*index2D( M, row, col ) = 1.0;
else
*index2D( M, row, col ) = 0.0;
}
}
Shallow vs Deep
As with any array in C, passing it around really only passes a reference (via the pointer to the array, or in our case, via the pointer to the array2D struct).
Anything you do to the array in a function modifies the source array.
If you want a true “deep” copy of the array, and not just a reference to it, you still have to do it the hard way.
You can (and should) write a function to help.
This is no different than you would have to do with any other array in C, no matter how you declare or obtain it.
array2D * copy2D( array2D * source )
{
array2D * result = create2D( source->rows, source->columns );
if (result)
{
for (int row = 0; row < source->rows; row += 1)
for (int col = 0; col < source->cols; col += 1)
*index2D( result, row, col ) = *index2D( source, row, col );
}
return result;
}
Honestly, that nested for loop could be replaced with a memcpy(), but you would have to do the hard stuff again and calculate the array size:
array2D * copy2D( array2D * source )
{
array2D * result = create2D( source->rows, source->columns );
if (result)
{
memcpy( result->values, source->values, sizeof(float) * source->rows * source->columns );
}
return result;
}
And you would have to free() the deep copy, just as you would any other array2D that you create.
This works the same as any other dynamically-allocated resource, array or not, in C:
array2D * a = create2D( 3, 4 ); // 'a' is a NEW array
array2D * b = copy2D( a ); // 'b' is a NEW array (copied from 'a')
array2D * c = a; // 'c' is an alias for 'a', not a copy
...
free( b ); // done with 'b'
free( a ); // done with 'a', also known as 'c'
That c reference thing is exactly how pointer and array arguments to functions work in C, so this should not be something surprising or new.
void myfunc( array2D * a ) // 'a' is an alias, not a copy
Hopefully you can see how easy it is to handle complex objects like variable-size arrays that keep their own size in C, with only a minor amount of work in one or two spots to manage such an object. This idea is called encapsulation (though without the data hiding aspect), and is one of the fundamental concepts behind OOP (and C++). Just because we’re using C doesn’t mean we can’t apply some of these concepts!
Finally, if you find the VLAs used in other answers to be more palatable or, more importantly, more correct or useful for your problem, then use them instead! In the end, what matters is that you find a solution that works and that satisfies your requirements.

FFTPACK in C examples

I'm trying to use FFTPACK converted from Fortran to C that I downloaded from Netlib (http://www.netlib.org/fftpack/). Unfortunately it seems to not really documented, and very cryptic (as I imagine most FFT codes are). Apparently it should follow a similar structure to the original Fortran code, so that's what I tried.
Here's what I have so far:
void main()
{
int n = 10;
float* wsave;
forward_transform(function1, wsave, n);
}
void forward_transform(float (*f)(float), float* wsave, int n)
{
int *ifac;
int i;
float r[n];
for (i = 0; i< n; i++)//set function values
{
r[i] = f((float)(-M_PI + i*2*M_PI/(n-1)));
}
__ogg_fdrffti(n, *wsave, *ifac);//initialize
__ogg_fdrfftf(n, *r, *wsave, *ifac);//forward transform
}
This code manages to compile, but gives a segfault when I call __ogg_fdrffti. I tried entering via gbd into fft.c to see exactly where the error is, but I can't seem to do that (the code still segfaults at the same line in my forward_transform function) leading me to believe that I'm somehow making an error in how I'm passing the various arrays.
Does anyone have any experience with or examples of the C version of FFTPACK?
The variables initialized in these functions have to exist somewhere in memory. You are passing pointers instead.
Try
void main()
{
int n = 10;
float wsave;
forward_transform(function1, wsave, n);
}
void forward_transform(float (*f)(float), float wsave, int n)
{
int ifac;
int i;
float r[n];
for (i = 0; i< n; i++)//set function values
{
r[i] = f((float)(-M_PI + i*2*M_PI/(n-1)));
}
__ogg_fdrffti(n, &wsave, &ifac);//initialize
__ogg_fdrfftf(n, r, &wsave, &ifac);//forward transform
}
Notice that the pointers are created using the address operator & on actual variables.

Malloc compile error: a value of type "int" cannot be used to initialize an entity of type int (*)[30]

I must have tried 20 ways of doing this by now. I really need help, no matter what I do i get a error similar to this one.
a value of type "int" cannot be used to initialize an entity of type "int (*)[30]"
i.e. this will get me such an error
int(*array)[160] = malloc((sizeof *array) * 10);
and doing something like this
int** Make2DintArray(int arraySizeX, int arraySizeY) {
int** theArray;
theArray = (int**) malloc(arraySizeX*sizeof(int*));
int i;
for (i = 0; i < arraySizeX; i++)
{
theArray[i] = (int*) malloc(arraySizeY*sizeof(int));
}
return theArray;
}
will get me this
"void *(size_t)" in "memory.c" at line 239 and: "int()"
does anyone have a solution for how to successful allocate a 2dArray of int[160][10]
Try this:
int **array;
array = malloc(rows * sizeof(int *));
for (i = 0; i < rows; i++)
array[i] = malloc(cols * sizeof(int));
// Some testing
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
array[i][j] = 0; // or whatever you want
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf("%d ", array[i][j]);
}
In your case rows = 160 and cols = 10. Is one possible solution.
With this approach you can use the two indexes:
Both of these compile fine for me. The first error is common when you forget to #include <stdlib.h> prior to using functions declared within said-same (such as malloc(size_t)), which I did not forget to do.
C has some interesting compile-time behaviors, among them the ability to invoke a function that has never been seen before (neither prototype definition nor implementation). Upon encountering such a call, C assumes the function is:
Something that returns int
Takes an unknown number of arguments, so the caller can pass whatever it wants (including the wrong things).
Eg., the function is implicitly assumed to be of the form:
int func();
Often you won't even notice, save for warnings from your compiler that report something to the effect of:
Warning: implicit declaration of `func` assumed to return `int`
and if you're on-the-ball, you have your warning levels turned up with warnings-as-errors enabled and will catch this.
But what if you don't? And what if the "thing" returned from the function cannot be content-represented by the data size in an implementation int ? What if, for example, int were 32-bit, but data pointers were 64-bit? For example, lets assume char *get_str() is declared in some header file you're not including, and implemented in a .c file you compile and link with your program, that looks like this:
#include <stdio.h>
// Note: NO prototype for get_str
int main()
{
char *s = get_str();
printf("String: %s\n", s);
return 0;
}
Well, the compiler should puke, telling you that int and char* are not compatible (shortly after it warns you get_str is assumed to return int). But what if you force the compiler's hand by telling it to make a char* one way or another:
#include <stdio.h>
// Note: NO prototype for get_str
int main()
{
char *s = (char*)get_str(); // NOTE: added cast
printf("String: %s\n", s);
return 0;
}
Now, without warnings-as-errors enabled, you'll get a implicit declaration warning, and thats it. The code will compile. But will it run ? If sizeof(int) != sizeof(char*), (32-bit vs 64-bit) likely not. The value returned from get_str is a 64-bit pointer, but the caller is assuming only 32-bits is returned, then forcing it to a 64-bit pointer. In short, the cast has hidden the error and opened pandora's box of undefined behavior.
So how does all of this relate to your code? By not including <stdlib.h> the compiler doesn't know what malloc is. So it assumes it is of the form:
int malloc();
Then, by casting the result to (int**) you're telling the compiler "whatever comes out of this, make it a int**". At link time, _malloc is found (no parameter signature via name mangling like C++), wired up, and your program is ready to party. But on your platform int and data pointers are not the same size, thus you end up with several undesirable consequences:
The cast hides the real error.
A bogus pointer is manufactured from half the bits of the real returned pointer.
As a cruel dose of salt to the wound, the allocated memory is leaked, as there are no valid pointers anywhere that reference it (you just destroyed the only one by only keeping half of it).
Probably the most undesirable, the code will exhibit normal behavior if compiled on an implementation where sizeof(int) == sizeof(int**).
So you build this on your 32-bit Debian box, all looks well. You turn in your homework to the professor who builds it on his 64bit Mac and it crashes, you fail the assignment, fail the class, drop out of college, and spend the next ten years petting the cat while watching Seinfeld reruns in your mom's basement wonder what went wrong. Ouch.
Don't treat casting like some silver bullet. It isn't. In C, it is needed far less often than people use it, and if used in the wrong place, can hide catastrophic errors. If you find a point in your code where something won't compile without a hard cast, look again. Unless you're absolutely, positively sure the cast is the right thing to do, odds are its wrong.
In this case it hid the real error, that you neglected to give enough info to your compiler to know what malloc really does.
To allocate the array:
int *array = malloc(sizeof(int) * 160 * 10);
Then use code like:
array[10 * row + column] = value;
(Where row goes from 0 to 159 inclusive and column goes from 0 to 9 inclusive.)
I have a note for rendon's answer:
For his code, Visual C++ says for every "=" operations: error C2440: '=' : cannot convert from 'void *' to 'int **'
By making some changes, it works for me, but I'm not sure that it does the same, so I afraid of editing his code. Instead, here's me code, that seems to work for a first impression.
int **a;
a = (int **)malloc(rows * sizeof(int));
for (i = 0; i < rows; i++)
{
a[i] = (int *)malloc(cols * sizeof(int));
}
for (j=0;j<rows;j++)
{
for (i=0;i<cols;i++)
{
a[i][j] = 2;
}
}
Actually, I did it with a custom struct instead of ints but I think either way should it work.
Don't mind me I'm just adding an example using calloc
void allocate_fudging_array(int R, int C)
{
int **fudging_array = (int **) calloc(R, sizeof(int *));
for(int k = 0; k < R; k++)
{
fudging_array[k] = (int*) calloc(C, sizeof(int));
}
}
// a helper function to print the array
void print2darr(int **arr, int R, int C)
{
for(int i = 0; i < R; i++)
{
for(int j = 0; j < C; j++)
{
printf(" %d ", arr[i][j]);
}
printf("\n");
}
}
2D array to store char *
char ***array;
int rows = 2, cols = 2, i, j;
array = malloc(sizeof(char **)*rows);
for (i = 0; i < rows; i++)
array[i] = malloc(cols * sizeof(char *));
array[0][0] = "asd";
array[0][1] = "qwe";
array[1][0] = "stack";
array[1][1] = "overflow";
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
printf("%s ",array[i][j]);
}
printf("\n");
}

Resources