From bdbd90916b6da97c576fff2aefa1f6a5cd5bdad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Fri, 30 Sep 2011 09:39:54 +0200 Subject: [PATCH] Use portable array initialisation. Memset() and calloc() define arrays of bytes, not general objects. This is a lesson learned many times over by generations of C programmers... --- src/coarse_conn.c | 19 ++++++++++++++----- src/coarse_sys.c | 43 ++++++++++++++++++++++++++++++++----------- src/fsh_common_impl.c | 6 +++++- src/hash_set.c | 6 +++--- src/ifsh_ms.c | 13 +++++++++---- src/partition.c | 19 ++++++++++++++----- src/sparse_sys.c | 5 ++++- src/well.c | 8 ++++++-- 8 files changed, 87 insertions(+), 32 deletions(-) diff --git a/src/coarse_conn.c b/src/coarse_conn.c index e4695860..3df30f47 100644 --- a/src/coarse_conn.c +++ b/src/coarse_conn.c @@ -492,6 +492,7 @@ coarse_topology_build(int ncoarse_f, int nblk, struct block_neighbours **bns) /* ---------------------------------------------------------------------- */ { + int i; int subface_valid; size_t nblkf, nsubf; struct coarse_topology *new; @@ -499,7 +500,7 @@ coarse_topology_build(int ncoarse_f, int nblk, new = malloc(1 * sizeof *new); if (new != NULL) { new->neighbours = malloc(2 * ncoarse_f * sizeof *new->neighbours); - new->blkfacepos = calloc(nblk + 1 , sizeof *new->blkfacepos); + new->blkfacepos = malloc((nblk + 1) * sizeof *new->blkfacepos); new->blkfaces = NULL; new->subfacepos = NULL; @@ -510,19 +511,27 @@ coarse_topology_build(int ncoarse_f, int nblk, coarse_topology_destroy(new); new = NULL; } else { - memset(new->neighbours, INT_MIN, - 2 * ncoarse_f * sizeof *new->neighbours); + for (i = 0; i < 2 * ncoarse_f; i++) { + new->neighbours[i] = INT_MIN; + } + for (i = 0; i < nblk + 1; i++) { + new->blkfacepos[i] = 0; + } coarse_topology_build_coarsef(nblk, bns, new->neighbours, new->blkfacepos, &nblkf, &nsubf); if (nsubf > 0) { - new->subfacepos = calloc(ncoarse_f + 1, sizeof *new->subfacepos); - new->subfaces = malloc(nsubf * sizeof *new->subfaces); + new->subfacepos = malloc((ncoarse_f + 1) * sizeof *new->subfacepos); + new->subfaces = malloc(nsubf * sizeof *new->subfaces); if ((new->subfacepos == NULL) || (new->subfaces == NULL)) { free(new->subfaces); new->subfaces = NULL; free(new->subfacepos); new->subfacepos = NULL; + } else { + for (i = 0; i < ncoarse_f + 1; i++) { + new->subfacepos[i] = 0; + } } } diff --git a/src/coarse_sys.c b/src/coarse_sys.c index 13d8987b..b2137d37 100644 --- a/src/coarse_sys.c +++ b/src/coarse_sys.c @@ -131,7 +131,7 @@ coarse_sys_meta_allocate(size_t nblocks, size_t nfaces_c, size_t nc , size_t nfaces_f) /* ---------------------------------------------------------------------- */ { - size_t alloc_sz; + size_t i, alloc_sz; struct coarse_sys_meta *new; new = malloc(1 * sizeof *new); @@ -147,13 +147,17 @@ coarse_sys_meta_allocate(size_t nblocks, size_t nfaces_c, alloc_sz += nfaces_c; /* bfno */ alloc_sz += 2*nfaces_c; /* loc_dofno */ - new->data = calloc(alloc_sz, sizeof *new->data); + new->data = malloc(alloc_sz * sizeof *new->data); if (new->data == NULL) { coarse_sys_meta_destroy(new); new = NULL; } else { + for (i = 0; i < alloc_sz; i++) { + new->data[i] = 0; + } + new->blk_nhf = new->data; new->blk_nfsf = new->blk_nhf + nblocks; new->ncf = new->blk_nfsf + nblocks; @@ -294,7 +298,9 @@ enumerate_active_bf(struct coarse_topology *ct, { int act, cf, b_in, b_out, b1, b2, p; - memset(m->bfno, -1, ct->nfaces * sizeof *m->bfno); + for (p = 0; p < ct->nfaces; p++) { + m->bfno[p] = -1; + } act = 0; @@ -330,7 +336,9 @@ compute_loc_dofno(struct coarse_topology *ct, { int b, nb, p, cf, locno, col_off; - memset(m->loc_dofno, -1, 2 * ct->nfaces * sizeof *m->loc_dofno); + for (p = 0; p < 2 * ct->nfaces; p++) { + m->loc_dofno[p] = -1; + } nb = ct->nblocks; @@ -401,7 +409,9 @@ coarse_sys_meta_fill(int nc, const int *pgconn, } } - memset(m->loc_fno, -1, nneigh * sizeof *m->loc_fno); + for (f = 0; f < nneigh; f++) { + m->loc_fno[f] = -1; + } m->max_cf_nf = 0; @@ -587,9 +597,11 @@ enforce_explicit_source(size_t nc, size_t nb, const int *p, int *has_src; ret = 0; - has_src = calloc(nb, sizeof *has_src); + has_src = malloc(nb * sizeof *has_src); if (has_src != NULL) { + for (b = 0; b < nb; b++) { has_src[b] = 0; } + for (c = 0; c < nc; c++) { has_src[p[c]] += fabs(src[c]) > 0.0; } @@ -709,9 +721,11 @@ blkdof_fill(struct coarse_topology *ct, nb = ct->nblocks; - sys->blkdof_pos = calloc(nb + 1, sizeof *sys->blkdof_pos); + sys->blkdof_pos = malloc((nb + 1) * sizeof *sys->blkdof_pos); if (sys->blkdof_pos != NULL) { + for (b = 0; b <= nb; b++) { sys->blkdof_pos[b] = 0; } + /* Count number of active BFs per block */ for (b = 0, p = 0; b < nb; b++) { for (; p < ct->blkfacepos[b + 1]; p++) { @@ -799,7 +813,7 @@ coarse_sys_allocate(struct coarse_topology *ct, /* ---------------------------------------------------------------------- */ { int alloc_ok; - size_t nb; + size_t i, nb; size_t bf_asz, ip_asz, Binv_asz; /* Allocation sizes */ struct coarse_sys *new; @@ -816,8 +830,8 @@ coarse_sys_allocate(struct coarse_topology *ct, new->dof2conn = malloc(m->n_act_bf * sizeof *new->dof2conn); - new->basis_pos = calloc(nb + 1, sizeof *new->basis_pos ); - new->cell_ip_pos = calloc(nb + 1, sizeof *new->cell_ip_pos); + new->basis_pos = malloc((nb + 1) * sizeof *new->basis_pos ); + new->cell_ip_pos = malloc((nb + 1) * sizeof *new->cell_ip_pos); new->basis = malloc(bf_asz * sizeof *new->basis ); new->cell_ip = malloc(ip_asz * sizeof *new->cell_ip); @@ -834,6 +848,11 @@ coarse_sys_allocate(struct coarse_topology *ct, if (alloc_ok < 7) { coarse_sys_destroy(new); new = NULL; + } else { + for (i = 0; i <= nb; i++) { + new->basis_pos [i] = 0; + new->cell_ip_pos[i] = 0; + } } } @@ -1211,7 +1230,9 @@ symmetrise_flux(size_t cf, grid_t *g, struct coarse_topology *ct, cnt = bf_asm->fcount; vector_zero(bf_asm->A->m, flux); - memset(cnt, 0, bf_asm->A->m * sizeof *bf_asm->fcount); + for (f = 0; f < bf_asm->A->m; f++) { + cnt[f] = 0; + } /* Accumulate (and count) number of fine-scale flux contributions * from this particular BF. */ diff --git a/src/fsh_common_impl.c b/src/fsh_common_impl.c index b37e650c..28ba65b9 100644 --- a/src/fsh_common_impl.c +++ b/src/fsh_common_impl.c @@ -221,7 +221,11 @@ void fsh_define_cell_wells(size_t nc, well_t *W, struct fsh_impl *pimpl) /* ---------------------------------------------------------------------- */ { - memset(pimpl->cwell_pos, 0, (nc + 1) * sizeof *pimpl->cwell_pos); + size_t i; + + for (i = 0; i < nc + 1; i++) { + pimpl->cwell_pos[i] = 0; + } derive_cell_wells(nc, W, pimpl->cwell_pos, pimpl->cwells); } diff --git a/src/hash_set.c b/src/hash_set.c index 2bcff5e3..a8737323 100644 --- a/src/hash_set.c +++ b/src/hash_set.c @@ -127,7 +127,7 @@ hash_set_expand(size_t m, struct hash_set *t) s = malloc(m * sizeof *s); if (s != NULL) { - memset(s, -1, m * sizeof *s); + for (i = 0; i < m; i++) { s[i] = -1; } for (i = 0; i < t->m; i++) { ret = hash_set_insert_core(t->s[i], m, s); @@ -169,7 +169,7 @@ struct hash_set * hash_set_allocate(int m) /* ---------------------------------------------------------------------- */ { - size_t sz; + size_t i, sz; struct hash_set *new; new = malloc(1 * sizeof *new); @@ -182,7 +182,7 @@ hash_set_allocate(int m) new = NULL; } else { - memset(new->s, -1, sz * sizeof *new->s); + for (i = 0; i < sz; i++) { new->s[i] = -1; } new->m = sz; } } diff --git a/src/ifsh_ms.c b/src/ifsh_ms.c index 20c40cf3..fc49e88a 100644 --- a/src/ifsh_ms.c +++ b/src/ifsh_ms.c @@ -90,15 +90,17 @@ max_block_cells(size_t nc, size_t nb, const int *p) /* ---------------------------------------------------------------------- */ { int ret, *cnt; - size_t c; + size_t b, c; ret = -1; - cnt = calloc(nb, sizeof *cnt); + cnt = malloc(nb * sizeof *cnt); if (cnt != NULL) { ret = 0; + for (b = 0; b < nb; b++) { cnt[b] = 0; } + for (c = 0; c < nc; c++) { cnt[p[c]] += 1; ret = MAX(ret, cnt[p[c]]); @@ -376,6 +378,7 @@ ifsh_ms_construct(grid_t *G , LocalSolver linsolve) /* ---------------------------------------------------------------------- */ { + int i; struct ifsh_ms_data *new; new = malloc(1 * sizeof *new); @@ -400,8 +403,10 @@ ifsh_ms_construct(grid_t *G , set_impl_pointers(G, new); memcpy(new->pimpl->p, p, G->number_of_cells * sizeof *p); - memset(new->pimpl->pb2c, 0, - new->pimpl->ct->nblocks * sizeof *new->pimpl->pb2c); + + for (i = 0; i < new->pimpl->ct->nblocks; i++) { + new->pimpl->pb2c[i] = 0; + } partition_invert(G->number_of_cells, new->pimpl->p, new->pimpl->pb2c, new->pimpl->b2c); diff --git a/src/partition.c b/src/partition.c index a1cbe2ad..a056258f 100644 --- a/src/partition.c +++ b/src/partition.c @@ -143,10 +143,11 @@ partition_compress(int n, int *p) max = MAX(max, p[i]); } - compr = calloc(max + 1, sizeof *compr); + compr = malloc((max + 1) * sizeof *compr); if (compr != NULL) { - for (i = 0; i < n; i++) { compr[p[i]]++; } + for (i = 0; i < max + 1; i++) { compr[i] = 0; } + for (i = 0; i < n; i++) { compr[p[i]]++; } compr[0] = -1 + (compr[0] > 0); for (i = 1; i <= max; i++) { @@ -242,7 +243,9 @@ partition_invert(int nc, const int *p, int *pi, int *inverse) nbin = max_block(nc, p) + 1; /* Adjust for bin 0 */ /* Zero start pointers */ - memset(pi, 0, (nbin + 1) * sizeof *pi); + for (i = 0; i < nbin + 1; i++) { + pi[i] = 0; + } /* Count elements per bin */ for (i = 0; i < nc; i++) { pi[ p[i] + 1 ]++; } @@ -310,9 +313,13 @@ partition_create_c2c(int nc, int nneigh, const int *neigh, { int i, ret, c1, c2; - *pc2c = calloc(nc + 1, sizeof **pc2c); + *pc2c = malloc((nc + 1) * sizeof **pc2c); if (*pc2c != NULL) { + for (i = 0; i < nc + 1; i++) { + (*pc2c)[i] = 0; + } + for (i = 0; i < nneigh; i++) { c1 = neigh[2*i + 0]; c2 = neigh[2*i + 1]; @@ -457,7 +464,9 @@ create_block_conns(int b , nc = pb2c[b + 1] - pb2c[b]; /* Clear start pointers */ - memset(ia, 0, (nc + 1) * sizeof *ia); + for (i = 0; i < nc + 1; i++) { + ia[i] = 0; + } for (i = pb2c[b]; i < pb2c[b + 1]; i++) { c = b2c[i]; assert (loc[c] == i - pb2c[b]); diff --git a/src/sparse_sys.c b/src/sparse_sys.c index 77637ee6..d3e97df6 100644 --- a/src/sparse_sys.c +++ b/src/sparse_sys.c @@ -29,15 +29,18 @@ struct CSRMatrix * csrmatrix_new_count_nnz(size_t m) /* ---------------------------------------------------------------------- */ { + size_t i; struct CSRMatrix *new; assert (m > 0); new = malloc(1 * sizeof *new); if (new != NULL) { - new->ia = calloc(m + 1, sizeof *new->ia); + new->ia = malloc((m + 1) * sizeof *new->ia); if (new->ia != NULL) { + for (i = 0; i < m + 1; i++) { new->ia[i] = 0; } + new->m = m; new->nnz = 0; diff --git a/src/well.c b/src/well.c index 785cf15f..2667391d 100644 --- a/src/well.c +++ b/src/well.c @@ -42,11 +42,11 @@ int allocate_cell_wells(int nc, well_t *W, int **cwpos, int **cwells) /* ---------------------------------------------------------------------- */ { - int totwconn; + int i, totwconn; totwconn = W->well_connpos[W->number_of_wells]; - *cwpos = calloc(nc + 1 , sizeof **cwpos ); + *cwpos = malloc((nc + 1) * sizeof **cwpos ); *cwells = malloc(2 * totwconn * sizeof **cwells); if ((*cwpos == NULL) || (*cwells == NULL)) { @@ -56,6 +56,10 @@ allocate_cell_wells(int nc, well_t *W, int **cwpos, int **cwells) *cwells = NULL; totwconn = 0; + } else { + for (i = 0; i < nc + 1; i++) { + (*cwpos)[i] = 0; + } } return totwconn;