Skip to content

Commit 71e27ba

Browse files
authored
Fixing C warnings (#934)
* Added warnings as errors for C/C++ code and fixed C warnings.
1 parent 0aa5333 commit 71e27ba

File tree

10 files changed

+54
-54
lines changed

10 files changed

+54
-54
lines changed

SConstruct

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ env = Environment(ENV=os.environ,
2323
'Go': go_builder},
2424
tools=['gcc', 'gnulink', 'g++', 'gas', 'gfortran'])
2525

26-
env['CCFLAGS'] = ''
26+
env['CCFLAGS'] = '-Wall -Wextra -Werror'
2727
env['CXXFLAGS'] = '-std=c++17'
2828
env['ASFLAGS'] = '--64'
2929

contents/IFS/code/c/IFS.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void chaos_game(struct point *in, size_t in_n, struct point *out,
2020

2121
struct point cur_point = {drand(), drand()};
2222

23-
for (int i = 0; i < out_n; ++i) {
23+
for (size_t i = 0; i < out_n; ++i) {
2424
out[i] = cur_point;
2525
struct point tmp = random_element(in, in_n);
2626
cur_point.x = 0.5 * (cur_point.x + tmp.x);

contents/barnsley/code/c/barnsley.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct matrix select_array(struct matrix *hutchinson_op, double *probabilities,
4444
}
4545
rnd -= probabilities[i];
4646
}
47+
return hutchinson_op[0];
4748
}
4849

4950
// This is a general function to simulate a chaos game

contents/cooley_tukey/code/c/fft.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <time.h>
77
#include <fftw3.h>
88

9-
void fft(double complex *x, int n) {
9+
void fft(double complex *x, size_t n) {
1010
double complex y[n];
1111
memset(y, 0, sizeof(y));
1212
fftw_plan p;
@@ -56,8 +56,8 @@ void cooley_tukey(double complex *X, const size_t N) {
5656
}
5757

5858
void bit_reverse(double complex *X, size_t N) {
59-
for (int i = 0; i < N; ++i) {
60-
int n = i;
59+
for (size_t i = 0; i < N; ++i) {
60+
size_t n = i;
6161
int a = i;
6262
int count = (int)log2((double)N) - 1;
6363

@@ -81,7 +81,7 @@ void iterative_cooley_tukey(double complex *X, size_t N) {
8181
bit_reverse(X, N);
8282

8383
for (int i = 1; i <= log2((double)N); ++i) {
84-
int stride = pow(2, i);
84+
size_t stride = pow(2, i);
8585
double complex w = cexp(-2.0 * I * M_PI / stride);
8686
for (size_t j = 0; j < N; j += stride) {
8787
double complex v = 1.0;
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
#include <stdio.h>
2-
#include <math.h>
2+
#include <stdlib.h>
33

44
int euclid_mod(int a, int b) {
5-
a = abs(a);
6-
b = abs(b);
5+
a = abs(a);
6+
b = abs(b);
77

8-
while (b != 0) {
9-
int temp = b;
10-
b = a % b;
11-
a = temp;
12-
}
8+
while (b != 0) {
9+
int temp = b;
10+
b = a % b;
11+
a = temp;
12+
}
1313

14-
return a;
14+
return a;
1515
}
1616

1717
int euclid_sub(int a, int b) {
18-
a = abs(a);
19-
b = abs(b);
20-
21-
while (a != b) {
22-
if (a > b) {
23-
a -= b;
24-
} else {
25-
b -= a;
26-
}
18+
a = abs(a);
19+
b = abs(b);
20+
21+
while (a != b) {
22+
if (a > b) {
23+
a -= b;
24+
} else {
25+
b -= a;
2726
}
27+
}
2828

29-
return a;
29+
return a;
3030
}
3131

3232
int main() {
33-
int check1 = euclid_mod(64 * 67, 64 * 81);
34-
int check2 = euclid_sub(128 * 12, 128 * 77);
33+
int check1 = euclid_mod(64 * 67, 64 * 81);
34+
int check2 = euclid_sub(128 * 12, 128 * 77);
3535

36-
printf("%d\n", check1);
37-
printf("%d\n", check2);
36+
printf("%d\n", check1);
37+
printf("%d\n", check2);
3838

39-
return 0;
39+
return 0;
4040
}

contents/flood_fill/code/c/flood_fill.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int inbounds(struct point p, struct canvas c) {
2525
return (p.x < 0 || p.y < 0 || p.y >= c.max_y || p.x >= c.max_x) ? 0 : 1;
2626
}
2727

28-
int find_neighbors(struct canvas c, struct point p, int old_val, int new_val,
28+
int find_neighbors(struct canvas c, struct point p, int old_val,
2929
struct point *neighbors) {
3030
int cnt = 0;
3131
struct point points[4] = {
@@ -90,7 +90,7 @@ void stack_fill(struct canvas c, struct point p, int old_val, int new_val) {
9090
c.data[cur_loc.x + c.max_x * cur_loc.y] = new_val;
9191

9292
struct point neighbors[4];
93-
int cnt = find_neighbors(c, cur_loc, old_val, new_val, neighbors);
93+
int cnt = find_neighbors(c, cur_loc, old_val, neighbors);
9494

9595
for (int i = 0; i < cnt; ++i) {
9696
stack_push(&stk, neighbors[i]);
@@ -160,7 +160,7 @@ void queue_fill(struct canvas c, struct point p, int old_val, int new_val) {
160160
c.data[cur_loc.x + c.max_x * cur_loc.y] = new_val;
161161

162162
struct point neighbors[4];
163-
int cnt = find_neighbors(c, cur_loc, old_val, new_val, neighbors);
163+
int cnt = find_neighbors(c, cur_loc, old_val, neighbors);
164164

165165
for (int i = 0; i < cnt; ++i) {
166166
enqueue(&q, neighbors[i]);
@@ -181,7 +181,7 @@ void recursive_fill(struct canvas c, struct point p, int old_val,
181181
c.data[p.x + c.max_x * p.y] = new_val;
182182

183183
struct point neighbors[4];
184-
int cnt = find_neighbors(c, p, old_val, new_val, neighbors);
184+
int cnt = find_neighbors(c, p, old_val, neighbors);
185185

186186
for (int i = 0; i < cnt; ++i) {
187187
recursive_fill(c, neighbors[i], old_val, new_val);

contents/gaussian_elimination/code/c/gaussian_elimination.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,31 @@ void gaussian_elimination(double *a, const size_t rows, const size_t cols) {
4747
}
4848
}
4949

50-
void back_substitution(const double *a, double *x, const size_t rows,
51-
const size_t cols) {
50+
void back_substitution(const double *a, double *x, const int rows,
51+
const int cols) {
5252

5353
for (int i = rows - 1; i >= 0; --i) {
5454
double sum = 0.0;
5555

56-
for (size_t j = cols - 2; j > i; --j) {
56+
for (int j = cols - 2; j > i; --j) {
5757
sum += x[j] * a[i * cols + j];
5858
}
5959

6060
x[i] = (a[i * cols + cols - 1] - sum) / a[i * cols + i];
6161
}
6262
}
6363

64-
void gauss_jordan(double *a, const size_t rows, const size_t cols) {
65-
int row = 0;
64+
void gauss_jordan(double *a, const size_t cols) {
65+
size_t row = 0;
6666

67-
for (int col = 0; col < cols - 1; ++col) {
67+
for (size_t col = 0; col < cols - 1; ++col) {
6868
if (a[row * cols + col] != 0) {
69-
for (int i = cols - 1; i > col - 1; --i) {
69+
for (size_t i = cols - 1; i > col - 1; --i) {
7070
a[row * cols + i] /= a[row * cols + col];
7171
}
7272

73-
for (int i = 0; i < row; ++i) {
74-
for (int j = cols - 1; j > col - 1; --j) {
73+
for (size_t i = 0; i < row; ++i) {
74+
for (size_t j = cols - 1; j > col - 1; --j) {
7575
a[i * cols + j] -= a[i * cols + col] * a[row * cols + j];
7676
}
7777
}
@@ -99,7 +99,7 @@ int main() {
9999

100100
printf("\nGauss-Jordan:\n");
101101

102-
gauss_jordan((double *)a, 3, 4);
102+
gauss_jordan((double *)a, 4);
103103

104104
for (size_t i = 0; i < 3; ++i) {
105105
printf("[");

contents/huffman_encoding/code/c/huffman.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ struct tree* generate_tree(const char* str) {
125125
}
126126

127127
struct heap heap = { 0 };
128-
for (int i = 0; i < sizeof(counts) / sizeof(int); ++i) {
128+
for (size_t i = 0; i < sizeof(counts) / sizeof(int); ++i) {
129129
if (counts[i]) {
130130
struct tree* tree = calloc(1, sizeof(struct tree));
131131
tree->value = (char)i;
@@ -211,8 +211,6 @@ char* encode(const char* input, struct tree** huffman_tree,
211211
*codebook = generate_codebook(*huffman_tree);
212212

213213
char* result = duplicate(get_code(codebook, *input));
214-
int result_length = strlen(result);
215-
int result_capacity = result_length;
216214

217215
input += 1;
218216

contents/split-operator_method/code/c/split_op.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct operators {
2828
double complex *wfc;
2929
};
3030

31-
void fft(double complex *x, int n, bool inverse) {
31+
void fft(double complex *x, size_t n, bool inverse) {
3232
double complex y[n];
3333
memset(y, 0, sizeof(y));
3434
fftw_plan p;
@@ -139,8 +139,8 @@ void split_op(struct params par, struct operators opr) {
139139
sprintf(filename, "output%lu.dat", i);
140140
FILE *fp = fopen(filename, "w");
141141

142-
for (int i = 0; i < opr.size; ++i) {
143-
fprintf(fp, "%d\t%f\t%f\n", i, density[i], creal(opr.v[i]));
142+
for (size_t i = 0; i < opr.size; ++i) {
143+
fprintf(fp, "%ld\t%f\t%f\n", i, density[i], creal(opr.v[i]));
144144
}
145145

146146
fclose(fp);

contents/stable_marriage_problem/code/c/stable_marriage.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ void shuffle(size_t *array, size_t size) {
2020
}
2121
}
2222

23-
void create_group(struct person *group, size_t size, bool are_men) {
23+
void create_group(struct person *group, size_t size) {
2424
for (size_t i = 0; i < size; ++i) {
2525
group[i].id = i;
2626
group[i].partner = NULL;
27-
group[i].prefers = (size_t*)malloc(sizeof(size_t) * size);
27+
group[i].prefers = malloc(sizeof(size_t) * size);
2828
group[i].index = 0;
2929

3030
for (size_t j = 0; j < size; ++j) {
@@ -43,6 +43,7 @@ bool prefers_partner(size_t *prefers, size_t partner, size_t id, size_t size) {
4343
return false;
4444
}
4545
}
46+
return true;
4647
}
4748

4849
void stable_marriage(struct person *men, struct person *women, size_t size) {
@@ -85,8 +86,8 @@ int main() {
8586

8687
struct person men[5], women[5];
8788

88-
create_group(men, 5, true);
89-
create_group(women, 5, false);
89+
create_group(men, 5);
90+
create_group(women, 5);
9091

9192
for (size_t i = 0; i < 5; ++i) {
9293
printf("preferences of man %zu: ", i);

0 commit comments

Comments
 (0)