Fixed leaks.

This commit is contained in:
Nicolás A. Ortega 2016-12-10 11:20:01 +01:00
parent 3c8b9922fb
commit dd38b53e31
No known key found for this signature in database
GPG Key ID: 614272579C2070D1
2 changed files with 7 additions and 2 deletions

View File

@ -21,6 +21,9 @@ void initList(List *l) {
} }
void deInitList(List *l) { void deInitList(List *l) {
for(ulli i = 0; i < l->size; ++i) {
mpz_clear(l->list[i]);
}
free(l->list); free(l->list);
} }
@ -29,7 +32,7 @@ void addToList(List *l, mpz_t n) {
l->size += BLOCK_SIZE; l->size += BLOCK_SIZE;
if(unlikely(l->size == 0)) { if(unlikely(l->size == 0)) {
fprintf(stderr, fprintf(stderr,
"size has reached limit of `long long int' type!\n"); "Size has reached limit of `long long int' type!\n");
exit(1); exit(1);
} }
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size); void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);

View File

@ -40,7 +40,8 @@ int main(void) {
// Loop through found primes // Loop through found primes
for(ulli i = 0; i < primes.size; ++i) { for(ulli i = 0; i < primes.size; ++i) {
// If `num' is divisible by a prime then go to the next number // If `num' is divisible by a prime then go to the next number
if(mpz_divisible_p(num, primes.list[i]) != 0) goto nextPrime; if(mpz_divisible_p(num, primes.list[i]) != 0)
goto nextPrime;
} }
// `num' is a prime so we add it to the list and print it // `num' is a prime so we add it to the list and print it
@ -56,6 +57,7 @@ nextPrime:
mpz_add_ui(num, num, 2); mpz_add_ui(num, num, 2);
} while(likely(run)); } while(likely(run));
mpz_clear(num);
// Deinitialize the list // Deinitialize the list
deInitList(&primes); deInitList(&primes);
return 0; return 0;