【发布时间】:2021-09-23 14:50:20
【问题描述】:
我试图用 C 语言编写一个非常简单的压缩器,但在我的代码中遇到了错误“堆已损坏”。我调查了一下,错误似乎是因为这行:
ptr = (char*)malloc(count * sizeof(char));
当我将大小从 count 更改为 1000 时,它可以工作,我尝试调试并查看某处是否存在差异但我找不到它,我知道可能存在某种溢出但我不明白为什么以及是什么解决方案,而不是仅仅写一个大数字来修复它
这是我现在的代码:
#include <stdio.h>
#include <stdlib.h>
errno_t err;
int count =0;
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data) {
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
};
int* frequency(char* str) {
FILE* fptr;
err = fopen_s(&fptr, str, "r");
if (err != 0)
{
printf("The file wasn't opened\n");
exit(0);
}
int* ptr;
ptr = (int*)malloc(95 * sizeof(int));
if (ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
for (int i = 0; i < 95; i++) {
*(ptr + i) = 0;
}
char ch;
int index;
while ((ch = fgetc(fptr)) != EOF) {
index = (int)ch - 32;
(*(ptr+index))++;
}
err = fclose(fptr);
if (err != 0)
{
printf("The file wasn't closed\n");
exit(0);
}
for (int i = 0; i < 95; i++) {
printf("%d ", *(ptr+i));
}
return ptr;
}
void* letFrequency(int* occLet) {
for (int i = 0; i < 95; i++) // counts how many actually occur
{
if (*(occLet+i) > 0)
{
count++;
}
}
int* ptr;
ptr = (char*)malloc(count * sizeof(char));
if (ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
int max = 0;
int placement = 0;
for (int j = 0; j < count; j++) {
for (int i = 0; i < 95; i++) {
if (*(occLet+i) >= max)
{
max = *(occLet+i);
placement = i;
}
}
*(ptr+j) = (char)(placement + 32);
printf("%c", *(ptr +j));
*(occLet+placement) = 0;
max = 1;
}
return ptr;
}
void binaryMap(char* letFrq) {
struct node* rootzero = newNode(1);
struct node* rootone = newNode(0);
int leaveszero = 0;
int leavesone = 0;
if (count % 2 == 0) {
leavesone = count / 2;
leaveszero = count / 2;
}
else
{
leavesone = count / 2;
leaveszero = count / 2 + 1;
printf("%d", leaveszero);
}
}
int main() {
char str[70];
printf("Enter the name of the text file you want to compress: ");
scanf_s("%s", str, sizeof(str));
int* ptrFr;
char* ptrLetFr;
ptrFr = frequency(str);
ptrLetFr = letFrequency(ptrFr);
free(ptrFr);
binaryMap(ptrLetFr);
}
【问题讨论】:
-
为什么
int*ptr不是char*ptr? -
在 C 中不要强制转换
malloc()的返回值是void*所以没有必要。sizeof(char)定义为 1 - 所以这也是不必要的。但是ptr = malloc( count * sizeof(*ptr) ) ;是个好主意,因为它允许prt的类型更改最少的维护——避免类型错误。 -
而你读取的文件只包含值> 32和
-
是的,尤其是因为 ey 使用的是
*(ptr+j),它将跳转 4 个字节而不是 1 个字节,因为它是int* -
@MohamedAkram 谢谢!我什至没有注意到,它解决了这个问题:)
标签: c heap-memory heap-corruption