Обновить sk1/compressor.c
This commit is contained in:
parent
39d5838adc
commit
2e67cc1c44
@ -103,7 +103,7 @@ struct MinHeap* createAndBuildMinHeap(unsigned char data[], int freq[], int size
|
|||||||
return minHeap;
|
return minHeap;
|
||||||
}
|
}
|
||||||
void freeMinHeap(struct MinHeap* 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[i]);
|
||||||
}
|
}
|
||||||
free(minHeap->array);
|
free(minHeap->array);
|
||||||
@ -149,10 +149,12 @@ void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int t
|
|||||||
}
|
}
|
||||||
if (!(root->left) && !(root->right)) {
|
if (!(root->left) && !(root->right)) {
|
||||||
currentCode[top] = '\0';
|
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
|
// Updated compressFile function
|
||||||
void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int top) {
|
void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int top) {
|
||||||
if (!root) return;
|
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);
|
struct MinHeapNode* root = buildHuffmanTree(data, frequencies, size);
|
||||||
char* codes[256] = {0};
|
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);
|
storeCodes(root, codes, currentCode, 0);
|
||||||
|
|
||||||
FILE* outputFile = fopen(output_file_name, "wb");
|
FILE* outputFile = fopen(output_file_name, "wb");
|
||||||
|
Loading…
Reference in New Issue
Block a user