fix dynamic array allocations

This commit is contained in:
Krishna Vedala 2020-07-13 00:12:57 -04:00
parent 9642e1068d
commit f58916f8f5
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
1 changed files with 10 additions and 6 deletions

View File

@ -128,12 +128,13 @@ int main(void)
const int N2 = N + 10; const int N2 = N + 10;
// const char N = 50, N2 = N+10; /* length of numbers */ // const char N = 50, N2 = N+10; /* length of numbers */
char txt_buffer[N + 5]; /* temporary buffer */ char *txt_buffer =
uint8_t number[N]; /* array to store digits of a large number */ (char *)calloc(N + 5, sizeof(char)); /* temporary buffer */
uint8_t sum[N2]; /* array to store the sum of the large numbers. For uint8_t *number = (uint8_t *)calloc(
safety, we make it twice the length of a number. */ N, sizeof(uint8_t)); /* array to store digits of a large number */
uint8_t *sum = (uint8_t *)calloc(
memset(sum, 0, sizeof(sum)); /* initialize sum array with 0 */ 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 */ FILE *fp = fopen("num.txt", "rt"); /* open text file to read */
if (!fp) if (!fp)
@ -161,5 +162,8 @@ int main(void)
print_number(sum, N2, 10); print_number(sum, N2, 10);
fclose(fp); /* close file */ fclose(fp); /* close file */
free(txt_buffer);
free(sum);
free(number);
return 0; return 0;
} }