Skip to content

Commit

Permalink
fix dynamic array allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
kvedala committed Jul 13, 2020
1 parent 9642e10 commit f58916f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions project_euler/problem_13/sol1.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ int main(void)
const int N2 = N + 10;

// const char N = 50, N2 = N+10; /* length of numbers */
char txt_buffer[N + 5]; /* temporary buffer */
uint8_t number[N]; /* array to store digits of a large number */
uint8_t sum[N2]; /* array to store the sum of the large numbers. For
safety, we make it twice the length of a number. */

memset(sum, 0, sizeof(sum)); /* initialize sum array with 0 */
char *txt_buffer =
(char *)calloc(N + 5, sizeof(char)); /* temporary buffer */
uint8_t *number = (uint8_t *)calloc(
N, sizeof(uint8_t)); /* array to store digits of a large number */
uint8_t *sum = (uint8_t *)calloc(
N2, sizeof(uint8_t)); /* array to store the sum of the large
numbers. For safety, we make it twice the length of a number. */

FILE *fp = fopen("num.txt", "rt"); /* open text file to read */
if (!fp)
Expand Down Expand Up @@ -161,5 +162,8 @@ int main(void)
print_number(sum, N2, 10);

fclose(fp); /* close file */
free(txt_buffer);
free(sum);
free(number);
return 0;
}

0 comments on commit f58916f

Please sign in to comment.