Skip to content

Commit

Permalink
changed all mallocs to callocs
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Dec 22, 2013
1 parent eabdfc8 commit 115c76b
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 54 deletions.
12 changes: 6 additions & 6 deletions src/alloc_entries.c
Expand Up @@ -38,29 +38,29 @@ err_t alloc_entries(Ledger *ledger){

/* allocate entries */

ledger->entries = malloc(NFIELDS * sizeof(char**));
ledger->entries = calloc(NFIELDS, sizeof(char**));
for(i = 0; i < NFIELDS; ++i){
ledger->entries[i] = malloc(ledger->nrows * sizeof(char*));
ledger->entries[i] = calloc(ledger->nrows, sizeof(char*));
for(j = 0; j < ledger->nrows; ++j)
ledger->entries[i][j] = calloc(ENTRYSIZE, sizeof(char));
}

/* test if malloc worked */
/* test if calloc worked */

ret = LSUCCESS;
if(ledger->entries == NULL){
fprintf(stderr, "Error: malloc failed\n");
fprintf(stderr, "Error: calloc failed\n");
ret = LFAILURE;
} else {
for(i = 0; i < NFIELDS; ++i){
if(ledger->entries[i] == NULL){
fprintf(stderr, "Error: malloc failed\n");
fprintf(stderr, "Error: calloc failed\n");
ret = LFAILURE;
break;
}
for(j = 0; j < ledger->nrows; ++j)
if(ledger->entries[i][j] == NULL){
fprintf(stderr,"Error: malloc failed\n");
fprintf(stderr,"Error: calloc failed\n");
ret = LFAILURE;
break;
}
Expand Down
14 changes: 7 additions & 7 deletions src/alloc_totals.c
Expand Up @@ -33,43 +33,43 @@ err_t alloc_totals(Ledger *ledger){
/* allocate space for the numerical summaries (totals) */

if(ledger->credit_totals == NULL){
ledger->credit_totals = malloc(ledger->ncredits * sizeof(double*));
ledger->credit_totals = calloc(ledger->ncredits, sizeof(double*));
for(i = 0; i < ledger->ncredits; ++i)
ledger->credit_totals[i] = calloc(N_TOTALS, sizeof(double));
}

if(ledger->bank_totals == NULL){
ledger->bank_totals = malloc(ledger->nbanks * sizeof(double*));
ledger->bank_totals = calloc(ledger->nbanks, sizeof(double*));
for(i = 0; i < ledger->nbanks; ++i)
ledger->bank_totals[i] = calloc(N_TOTALS, sizeof(double));
}

if(ledger->partition_totals == NULL){
ledger->partition_totals = malloc(ledger->nbanks * sizeof(double*));
ledger->partition_totals = calloc(ledger->nbanks, sizeof(double*));
for(i = 0; i < ledger->nbanks; ++i)
ledger->partition_totals[i] = calloc(ledger->npartitions[i], sizeof(double));
}

/* check if malloc worked */
/* check if calloc worked */

ret = LSUCCESS;
if(ledger->credit_totals == NULL ||
ledger->bank_totals == NULL ||
ledger->partition_totals == NULL){
fprintf(stderr, "Error: malloc failed\n");
fprintf(stderr, "Error: calloc failed\n");
ret = LFAILURE;
} else {
for(i = 0; i < ledger->ncredits; ++i){
if(ledger->credit_totals[i] == NULL){
fprintf(stderr, "Error: malloc failed\n");
fprintf(stderr, "Error: calloc failed\n");
ret = LFAILURE;
break;
}
}

for(i = 0; i < ledger->nbanks; ++i){
if(ledger->bank_totals[i] == NULL || ledger->partition_totals[i] == NULL){
fprintf(stderr, "Error: malloc failed\n");
fprintf(stderr, "Error: calloc failed\n");
ret = LFAILURE;
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/get_entries_from_filename.c
Expand Up @@ -34,13 +34,13 @@ err_t get_entries_from_filename(Ledger *ledger, char *filename){
return LFAILURE;

if(ledger->filename == NULL)
ledger->filename = malloc(FILENAMESIZE * sizeof(char));
ledger->filename = calloc(FILENAMESIZE, sizeof(char));
strncpy(ledger->filename, filename, (ENTRYSIZE - 1) * sizeof(char));

/* Check if malloc worked */
/* Check if calloc worked */

if(ledger->filename == NULL){
fprintf(stderr, "Error: malloc failed\n");
fprintf(stderr, "Error: calloc failed\n");
if(fp != NULL)
fclose(fp);
return LFAILURE;
Expand Down
14 changes: 7 additions & 7 deletions src/get_names.c
Expand Up @@ -49,31 +49,31 @@ err_t get_names(Ledger *ledger){
/* Allocate arrays for partition names */

ledger->npartitions = calloc(ledger->nbanks, sizeof(int*));
ledger->partitions = malloc(ledger->nbanks * sizeof(char***));
ledger->partitions = calloc(ledger->nbanks, sizeof(char***));

/* Check if malloc worked */
/* Check if calloc worked */

if(ledger->partitions == NULL || ledger->npartitions == NULL){
fprintf(stderr, "Error: malloc failed\n");
fprintf(stderr, "Error: calloc failed\n");
return LFAILURE;
}

/* Allocate array for sorted partition names */

s = malloc(ledger->nrows * sizeof(char*));
s = calloc(ledger->nrows, sizeof(char*));
for(i = 0; i < ledger->nrows; ++i)
s[i] = calloc(ENTRYSIZE, sizeof(char));

/* Check if malloc worked */
/* Check if calloc worked */

ret = LSUCCESS;
if(s == NULL){
fprintf(stderr, "Error: malloc failed\n");
fprintf(stderr, "Error: calloc failed\n");
ret = LFAILURE;
} else {
for(i = 0; i < ledger->nrows; ++i)
if(s[i] == NULL){
fprintf(stderr, "Error: malloc failed\n");
fprintf(stderr, "Error: calloc failed\n");
ret = LFAILURE;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/insert_rows.c
Expand Up @@ -46,29 +46,29 @@ err_t insert_rows(Ledger *ledger, int row, int howmany){

/* Allocate space for new ledger entries */

x = malloc(NFIELDS * sizeof(char**));
x = calloc(NFIELDS, sizeof(char**));
for(i = 0; i < NFIELDS; ++i){
x[i] = malloc((ledger->nrows + howmany) * sizeof(char*));
x[i] = calloc((ledger->nrows + howmany), sizeof(char*));
for(j = 0; j < (ledger->nrows + howmany); ++j)
x[i][j] = calloc(ENTRYSIZE, sizeof(char));
}

/* Check if malloc worked */
/* Check if calloc worked */

ret = LSUCCESS;
if(x == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
} else {
for(i = 0; i < NFIELDS; ++i){
if(x[i] == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
ret = LFAILURE;
break;
}
for(j = 0; j < (ledger->nrows + howmany); ++j)
if(x[i][j] == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
ret = LFAILURE;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/move_rows.c
Expand Up @@ -31,11 +31,11 @@ err_t move_rows(Ledger *ledger, int *rows, int nrows, int moveto){
if(ledger->nrows < 1)
return LFAILURE;

/* Allocate space for permutation order and check if malloc worked */
/* Allocate space for permutation order and check if calloc worked */

order = calloc(ledger->nrows, sizeof(int));
if(order == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/new_ledger.c
Expand Up @@ -21,10 +21,10 @@
err_t new_ledger(Ledger **ledger){
free_ledger(ledger);

*ledger = malloc(sizeof(Ledger));
*ledger = calloc(1, sizeof(Ledger));

if(*ledger == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/print_ledger_to_string.c
Expand Up @@ -31,15 +31,15 @@ err_t print_ledger_to_string(Ledger *ledger, char **s){
if(strip_ledger(ledger) == LFAILURE)
return LFAILURE;

/* Allocate space for the output string and check that malloc worked */
/* Allocate space for the output string and check that calloc worked */

for(field = 0; field < NFIELDS; ++field)
for(row = 0; row < ledger->nrows; ++row)
n += strlen(ledger->entries[field][row]) + 1;

*s = calloc(n + LINESIZE, sizeof(char));
if(*s == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/print_summary_to_string.c
Expand Up @@ -39,11 +39,11 @@ err_t print_summary_to_string(Ledger *ledger, char **s, int usecolor){
return LFAILURE;
}

/* Allocate space for string and test if malloc worked */
/* Allocate space for string and test if calloc worked */

*s = calloc((ledger->nrows + 1) * NFIELDS * LINESIZE, sizeof(char));
if(*s == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/remove_rows.c
Expand Up @@ -29,11 +29,11 @@ err_t remove_rows(Ledger *ledger){
if(ledger->entries == NULL)
return LFAILURE;

/* Allocate space for permutation and check if malloc worked */
/* Allocate space for permutation and check if calloc worked */

order = calloc(ledger->nrows, sizeof(int));
if(order == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

Expand Down
6 changes: 3 additions & 3 deletions src/repartition.c
Expand Up @@ -45,14 +45,14 @@ err_t repartition(Ledger *ledger, char *bank, char **partitions,
if(ibank == NO_INDEX)
return LFAILURE;

/* Allocate space for new amounts and check if malloc worked */
/* Allocate space for new amounts and check if calloc worked */

amounts = malloc(npartitions * sizeof(double));
amounts = calloc(npartitions, sizeof(double));
for(i = 0; i < npartitions; ++i)
amounts[i] = amounts_arg[i];

if(amounts == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/sort_by_status.c
Expand Up @@ -34,11 +34,11 @@ err_t sort_by_status(Ledger *ledger, int sort_locked){
if(ledger->nrows < 1)
return LFAILURE;

/* Allocate space for permutation and check if malloc worked */
/* Allocate space for permutation and check if calloc worked */

order = calloc(ledger->nrows, sizeof(int));
if(order == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

Expand Down
20 changes: 10 additions & 10 deletions src/unique.c
Expand Up @@ -31,17 +31,17 @@ err_t unique(char **a, int n, char ***ret, int *nunique){
if(a == NULL || n < 1)
return LFAILURE;

/* Make a sanitized local copy of input and check if malloc worked */
/* Make a sanitized local copy of input and check if calloc worked */

s = malloc((n + 1) * sizeof(char*));
s = calloc((n + 1), sizeof(char*));
if(s == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

s[0] = calloc(ENTRYSIZE, sizeof(char));
if(s[0] == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

Expand All @@ -51,7 +51,7 @@ err_t unique(char **a, int n, char ***ret, int *nunique){
s[j] = calloc(ENTRYSIZE, sizeof(char));

if(s[j] == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");

for(k = 0; k < j; ++k)
if(s[k] != NULL)
Expand All @@ -66,7 +66,7 @@ err_t unique(char **a, int n, char ***ret, int *nunique){
a[j - 1] = calloc(ENTRYSIZE, sizeof(char));

if(a[j - 1] == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
for(k = 0; k < n + 1; ++k)
if(s[k] != NULL)
free(s[k]);
Expand Down Expand Up @@ -96,18 +96,18 @@ err_t unique(char **a, int n, char ***ret, int *nunique){
++(*nunique);
}

/* Allocate space for the array of unique strings and check if malloc worked */
/* Allocate space for the array of unique strings and check if calloc worked */

*ret = malloc(*nunique * sizeof(char*));
*ret = calloc(*nunique, sizeof(char*));
if(*ret == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");
return LFAILURE;
}

for(i = 0; i < *nunique; ++i){
(*ret)[i] = calloc(ENTRYSIZE, sizeof(char));
if(*ret == NULL){
fprintf(stderr, "Error: malloc failed.\n");
fprintf(stderr, "Error: calloc failed.\n");

for(k = 0; k < i; ++k)
if((*ret)[k] != NULL)
Expand Down

0 comments on commit 115c76b

Please sign in to comment.