Обновить sk1/compressor.c

This commit is contained in:
Yevhen Kozirovskyi 2025-01-19 18:03:36 +00:00
parent 39d5838adc
commit 2e67cc1c44

View File

@ -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");