From 2e67cc1c44ed890555984cd9ad424248e1671b58 Mon Sep 17 00:00:00 2001 From: Yevhen Kozirovskyi Date: Sun, 19 Jan 2025 18:03:36 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20sk1/compressor.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sk1/compressor.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sk1/compressor.c b/sk1/compressor.c index ce8f33c..6be2ab1 100644 --- a/sk1/compressor.c +++ b/sk1/compressor.c @@ -103,7 +103,7 @@ struct MinHeap* createAndBuildMinHeap(unsigned char data[], int freq[], int size return minHeap; } void freeMinHeap(struct MinHeap* minHeap) { - for (int i = 0; i < minHeap->size; ++i) { + for (unsigned i = 0; i < minHeap->size; ++i) { // Changed to unsigned free(minHeap->array[i]); } free(minHeap->array); @@ -149,10 +149,12 @@ void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int t } if (!(root->left) && !(root->right)) { currentCode[top] = '\0'; - codes[root->data] = strdup(currentCode); // Ensure memory is allocated + codes[root->data] = (char*)malloc(strlen(currentCode) + 1); // Use malloc instead of strdup + strcpy(codes[root->data], currentCode); // Copy the string } } + // Updated compressFile function void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int top) { if (!root) return; @@ -199,10 +201,10 @@ int compressFile(const char* input_file_name, const char* output_file_name) { } } - struct MinHeap* minHeap = createAndBuildMinHeap(data, frequencies, size); // Создаем кучу + struct MinHeap* minHeap = createAndBuildMinHeap(data, frequencies, size); // Create heap struct MinHeapNode* root = buildHuffmanTree(data, frequencies, size); char* codes[256] = {0}; - char currentCode[MAX_TREE_HT] = {0}; // Initialize to avoid garbage values + char currentCode[MAX_TREE_HT] = {0}; // Initialize to avoid garbage values storeCodes(root, codes, currentCode, 0); FILE* outputFile = fopen(output_file_name, "wb");