-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmatvec.c
More file actions
34 lines (27 loc) · 1.03 KB
/
matvec.c
File metadata and controls
34 lines (27 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
/* return y = Ax */
int *matvec(int **A, int *x, size_t n) {
if (n == 0 || n > SIZE_MAX / sizeof(int))
return NULL; // n is either too large for calloc or 0
int *y = (int *)calloc(n, sizeof(int)); // calloc initializes to zero
if (y == NULL)
return NULL; // calloc failed when generating the y vector to hold the product
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
y[i] += A[i][j] * x[j];
return y;
}
/*
n, which represents the length of the the x vector and that of each row of A,
should be a size_t, not an int.
Need to prevent unsigned integer wrap in the call to calloc by checking
if n > SIZE_MAX / sizeof(int). If so, then n would be at least 16384, which
leads to wrap when mutiplied by sizeof(int) == 4.
We need to check the return value of calloc to ensure that the memory
allocation function succeeded and return NULL if it didn't.
Not sure if the matvec function should be responsible for checking if
A and x are valid pointers...
*/