From a77a9c06dc9f71ec08b4455a65c9ccdce63ad214 Mon Sep 17 00:00:00 2001 From: Nikola Date: Tue, 1 Sep 2026 19:10:07 +0300 Subject: [PATCH] FIXED: Memory leaks and unused color table --- Implementierung/main.c | 15 +++++++++------ Implementierung/tricorn.c | 6 +++--- Implementierung/tricorn_V2.c | 6 +++--- Implementierung/tricorn_V3.c | 1 - 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Implementierung/main.c b/Implementierung/main.c index f8b46c7..19d64a3 100644 --- a/Implementierung/main.c +++ b/Implementierung/main.c @@ -233,14 +233,16 @@ int main(int argc, char** argv) 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) { fprintf(stderr,"Memory allocation failed.\n"); return EXIT_FAILURE; } - strcpy(output_data,optarg); + strncpy(output_data,optarg,output_data_length); + output_data[output_data_length-1] = 0; break; case 'C': @@ -269,14 +271,15 @@ int main(int argc, char** argv) float complex start = (starting_point_real + starting_point_imaginary*I); int bytesPerColour = colored ? 3 : 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 if(imageArr == NULL) exit(EXIT_FAILURE); - - if (will_test) - return tests() ? EXIT_FAILURE : EXIT_SUCCESS; - + if (!(nGiven && rGiven && oGiven && dGiven && sGiven)) { checkInputs(nGiven,rGiven,oGiven,dGiven,sGiven); diff --git a/Implementierung/tricorn.c b/Implementierung/tricorn.c index 26e8617..c9679c6 100644 --- a/Implementierung/tricorn.c +++ b/Implementierung/tricorn.c @@ -120,9 +120,9 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig img[position] = value; } else { - img[position] = ((value*3) % 255); - img[position + 1] = ((value*2) % 255); - img[position + 2] = ((value*5) % 255); + img[position] = color_lookup_table[value][0]; + img[position + 1] = color_lookup_table[value][1]; + img[position + 2] = color_lookup_table[value][2]; } } // Accumulate the imaginary part diff --git a/Implementierung/tricorn_V2.c b/Implementierung/tricorn_V2.c index a954cbc..d3f57a6 100644 --- a/Implementierung/tricorn_V2.c +++ b/Implementierung/tricorn_V2.c @@ -120,9 +120,9 @@ void tricorn_V2(float complex start, size_t width, ssize_t height, float res, un img[position] = value; } else { - img[position] = ((value*3) % 255); - img[position + 1] = ((value*2) % 255); - img[position + 2] = ((value*5) % 255); + img[position] = color_lookup_table[value][0]; + img[position + 1] = color_lookup_table[value][1]; + img[position + 2] = color_lookup_table[value][2]; } } diff --git a/Implementierung/tricorn_V3.c b/Implementierung/tricorn_V3.c index 7ba3484..3e98ba0 100644 --- a/Implementierung/tricorn_V3.c +++ b/Implementierung/tricorn_V3.c @@ -72,7 +72,6 @@ void tricorn_V3(float complex start, size_t width, ssize_t height, float res, un args->consts = consts; if(pthread_create(&threads[i],NULL,compute,args)){ compute(args); - free(args); args = NULL; } }