无法释放 C 中的所有内存
Trouble freeing all memory in C
我正在使用最小堆对整数数组进行霍夫曼编码。我的问题是在我将霍夫曼代码打印到屏幕后释放 MinHeap 和 MinHeapNodes。每当我调用 "printCodes" 时,我都会释放 minHeap 的叶节点,但我仍然无法释放非叶节点以及 MinHeap 和 MinHeap 的数组。
#include <stdio.h>
#include <stdlib.h>
#define MAX_TREE_HT 100
struct MinHeapNode {
int data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode **array;
};
struct MinHeapNode *newNode(int data, unsigned freq) {
struct MinHeapNode *temp = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
struct MinHeap *createMinHeap(unsigned capacity) {
struct MinHeap *minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode**)malloc(minHeap->capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
void swapMinHeapNode(struct MinHeapNode **a, struct MinHeapNode **b) {
struct MinHeapNode *tmp = *a;
*a = *b;
*b = tmp;
}
void minHeapify(struct MinHeap *minHeap, int index) {
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != index) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[index]);
minHeapify(minHeap, smallest);
}
}
int isSizeOne(struct MinHeap *minHeap) {
return (minHeap->size == 1);
}
struct MinHeapNode *extractMin(struct MinHeap *minHeap) {
struct MinHeapNode *temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap *minHeap, struct MinHeapNode *minHeapNode) {
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
void buildMinHeap(struct MinHeap *minHeap) {
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d", arr[i]);
printf("\n");
}
int isLeaf(struct MinHeapNode *root) {
return !(root->left) && !(root->right);
}
struct MinHeap *createAndBuildMinHeap(int data[], int freq[], int size) {
struct MinHeap *minHeap = createMinHeap(size);
int i;
for (i = 0; i < size; i++)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHeapNode *buildHuffmanTree(int data[], int freq[], int size, struct MinHeap *clear) {
struct MinHeapNode *left, *right, *top;
struct MinHeap *minHeap = createAndBuildMinHeap(data, freq, size);
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode(255, left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
clear = minHeap;
//clear->array = minHeap->array
return extractMin(minHeap);
}
void printCodes(struct MinHeapNode *root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
printf("%d: ", root->data);
printArr(arr, top);
free(root);
}
}
void HuffmanCodes(int data[], int freq[], int size) {
struct MinHeap *clear;
struct MinHeapNode *root = buildHuffmanTree(data, freq, size, clear);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
//free(clear->array); Adding this will crash the program
//free(clear);
free(root);
}
int main() {
int arr[] = {3, 4, 5};
int freq[] = {1, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
HuffmanCodes(arr, freq, size);
return 0;
}
- 为了便于阅读,我会重命名您的 printCodes 函数 printCodesAndFree。
- 我猜,你会挠头然后撞到墙上。您正在将 clear 指针按值传递给 createAndBuildMinHeap 并设置局部变量。您应该传递 &clear 并在 buildHuffmanTree 中接收参数 struct MinHeap** clear,然后设置 *clear = minHeap。我想这会解决您的问题。
我正在使用最小堆对整数数组进行霍夫曼编码。我的问题是在我将霍夫曼代码打印到屏幕后释放 MinHeap 和 MinHeapNodes。每当我调用 "printCodes" 时,我都会释放 minHeap 的叶节点,但我仍然无法释放非叶节点以及 MinHeap 和 MinHeap 的数组。
#include <stdio.h>
#include <stdlib.h>
#define MAX_TREE_HT 100
struct MinHeapNode {
int data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode **array;
};
struct MinHeapNode *newNode(int data, unsigned freq) {
struct MinHeapNode *temp = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
struct MinHeap *createMinHeap(unsigned capacity) {
struct MinHeap *minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode**)malloc(minHeap->capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
void swapMinHeapNode(struct MinHeapNode **a, struct MinHeapNode **b) {
struct MinHeapNode *tmp = *a;
*a = *b;
*b = tmp;
}
void minHeapify(struct MinHeap *minHeap, int index) {
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != index) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[index]);
minHeapify(minHeap, smallest);
}
}
int isSizeOne(struct MinHeap *minHeap) {
return (minHeap->size == 1);
}
struct MinHeapNode *extractMin(struct MinHeap *minHeap) {
struct MinHeapNode *temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap *minHeap, struct MinHeapNode *minHeapNode) {
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
void buildMinHeap(struct MinHeap *minHeap) {
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d", arr[i]);
printf("\n");
}
int isLeaf(struct MinHeapNode *root) {
return !(root->left) && !(root->right);
}
struct MinHeap *createAndBuildMinHeap(int data[], int freq[], int size) {
struct MinHeap *minHeap = createMinHeap(size);
int i;
for (i = 0; i < size; i++)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHeapNode *buildHuffmanTree(int data[], int freq[], int size, struct MinHeap *clear) {
struct MinHeapNode *left, *right, *top;
struct MinHeap *minHeap = createAndBuildMinHeap(data, freq, size);
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode(255, left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
clear = minHeap;
//clear->array = minHeap->array
return extractMin(minHeap);
}
void printCodes(struct MinHeapNode *root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
printf("%d: ", root->data);
printArr(arr, top);
free(root);
}
}
void HuffmanCodes(int data[], int freq[], int size) {
struct MinHeap *clear;
struct MinHeapNode *root = buildHuffmanTree(data, freq, size, clear);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
//free(clear->array); Adding this will crash the program
//free(clear);
free(root);
}
int main() {
int arr[] = {3, 4, 5};
int freq[] = {1, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
HuffmanCodes(arr, freq, size);
return 0;
}
- 为了便于阅读,我会重命名您的 printCodes 函数 printCodesAndFree。
- 我猜,你会挠头然后撞到墙上。您正在将 clear 指针按值传递给 createAndBuildMinHeap 并设置局部变量。您应该传递 &clear 并在 buildHuffmanTree 中接收参数 struct MinHeap** clear,然后设置 *clear = minHeap。我想这会解决您的问题。