FIXED: Memory leaks and unused color table

This commit is contained in:
Nikola
2026-09-01 19:10:07 +03:00
parent f0207a75e8
commit a77a9c06dc
4 changed files with 15 additions and 13 deletions
+9 -6
View File
@@ -233,14 +233,16 @@ int main(int argc, char** argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
output_data = malloc(strlen(optarg) + 1); int output_data_length = strlen(optarg) + 1;
output_data = malloc(output_data_length);
if (output_data == NULL) if (output_data == NULL)
{ {
fprintf(stderr,"Memory allocation failed.\n"); fprintf(stderr,"Memory allocation failed.\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
strcpy(output_data,optarg); strncpy(output_data,optarg,output_data_length);
output_data[output_data_length-1] = 0;
break; break;
case 'C': case 'C':
@@ -269,14 +271,15 @@ int main(int argc, char** argv)
float complex start = (starting_point_real + starting_point_imaginary*I); float complex start = (starting_point_real + starting_point_imaginary*I);
int bytesPerColour = colored ? 3 : 1; int bytesPerColour = colored ? 3 : 1;
ssize_t absolute_height = height >= 0 ? height : height * - 1; ssize_t absolute_height = height >= 0 ? height : height * - 1;
if (will_test)
return tests() ? EXIT_FAILURE : EXIT_SUCCESS;
unsigned char *imageArr = malloc(width*absolute_height*bytesPerColour);// allocate image array on the heap to support large images as the stack is usually only 8 MB big unsigned char *imageArr = malloc(width*absolute_height*bytesPerColour);// allocate image array on the heap to support large images as the stack is usually only 8 MB big
if(imageArr == NULL) if(imageArr == NULL)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (will_test)
return tests() ? EXIT_FAILURE : EXIT_SUCCESS;
if (!(nGiven && rGiven && oGiven && dGiven && sGiven)) if (!(nGiven && rGiven && oGiven && dGiven && sGiven))
{ {
checkInputs(nGiven,rGiven,oGiven,dGiven,sGiven); checkInputs(nGiven,rGiven,oGiven,dGiven,sGiven);
+3 -3
View File
@@ -120,9 +120,9 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig
img[position] = value; img[position] = value;
} }
else { else {
img[position] = ((value*3) % 255); img[position] = color_lookup_table[value][0];
img[position + 1] = ((value*2) % 255); img[position + 1] = color_lookup_table[value][1];
img[position + 2] = ((value*5) % 255); img[position + 2] = color_lookup_table[value][2];
} }
} }
// Accumulate the imaginary part // Accumulate the imaginary part
+3 -3
View File
@@ -120,9 +120,9 @@ void tricorn_V2(float complex start, size_t width, ssize_t height, float res, un
img[position] = value; img[position] = value;
} }
else { else {
img[position] = ((value*3) % 255); img[position] = color_lookup_table[value][0];
img[position + 1] = ((value*2) % 255); img[position + 1] = color_lookup_table[value][1];
img[position + 2] = ((value*5) % 255); img[position + 2] = color_lookup_table[value][2];
} }
} }
-1
View File
@@ -72,7 +72,6 @@ void tricorn_V3(float complex start, size_t width, ssize_t height, float res, un
args->consts = consts; args->consts = consts;
if(pthread_create(&threads[i],NULL,compute,args)){ if(pthread_create(&threads[i],NULL,compute,args)){
compute(args); compute(args);
free(args);
args = NULL; args = NULL;
} }
} }