C语言 Malloc 无法分配内存



我的代码遇到了这个奇怪的问题。Malloc 在此处返回一个空指针。我的 ram 上有 3 GB 内存,它无法分配几个字节。为什么会这样??有人请帮忙..

这是我的代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct htree{
   unsigned char ch;
   unsigned long int freq;
   struct htree *left, *right, *parent;
   };
struct code_list{
   unsigned char ch;
   char *code;
   };

typedef struct htree node;
typedef struct code_list * dict;
dict codes;
int readfile(char *filename, long int *char_count)
{
 int types = 0;
 FILE *fp = fopen(filename,"rb");
 unsigned char byteread;
 fread(&byteread,1,1,fp);
 int count=0;
 while(!feof(fp))
 {
                 if (char_count[byteread] == 0)
                 {
                 types++;
                 }
                 char_count[byteread]++;
                 fread(&byteread,1,1,fp);
 }
 fclose(fp);
 return types-1;
}
node * genhuffnode()
{
 node *t = (node *)malloc(sizeof(struct htree));
 t -> ch = '';
 t -> freq = 0;
 t -> left = NULL;
 t -> right = NULL;
 t -> parent = NULL;
 return t;
}
node * genhufftree(long int *char_count, int max_index)
{
 node **stack;
 node *temp;
 stack = (node **) calloc(max_index,sizeof(node *));
 int i,j=0;
 for(i=0;i<256;i++)
 {
                   if(char_count[i]>0)
                   {
                   stack[j] = (node *)malloc(sizeof(node));
                   stack[j] -> ch = i;
                   stack[j] -> freq = char_count[i];
                   stack[j] -> left = NULL;
                   stack[j] -> right = NULL;
                   stack[j] -> parent = NULL;
                   j++;
                   }
 }
 for(i=0;i<=max_index;i++)
 for(j=i+1;j<=max_index;j++)
 if(stack[j] -> freq > stack[i] -> freq)
 {
                 temp = stack[j];
                 stack[j] = stack[i];
                 stack[i] = temp;
 }
 while(i>0)
 {
           temp = genhuffnode();
           temp -> freq = stack[i] -> freq + stack[i-1] -> freq;
           temp -> left = stack[i-1];
           temp -> right = stack[i];
           stack[i-1] -> parent = temp;
           stack[i] -> parent = temp;
           for(j=i-2;j>0;j--)
           {
                              if(temp->freq > stack[j-1]->freq)
                              stack[j+1] = stack[j];
                              else break;
           }
           stack[j] = temp;
           i--;
 }
 return stack[0];
}
void generatedict(node *root, char *s)
{
 if(root == NULL)
 return;
 char *new_code;
 static int index = 0;
 int len = strlen(s)+1;
 if(root->left == NULL && root->right == NULL)
 {
               codes[index].ch = root->ch;
               codes[index].code = (char *) malloc(len*sizeof(char));
               strcpy(codes[index].code,s);
               index++;
 }
 else
 {
               new_code = (char *)(malloc(len+1));/// this malloc is causing prob
               if(new_code == NULL)
               {
               printf("Coudnt allocate memoryn");
               getchar();
               exit(1);
               }
               else 
               {
               strcpy(new_code,s);
               new_code[len] = '';
               new_code[len-1] = '0';
               generatedict(root->left,new_code);
               new_code[len-1] = '1';
               generatedict(root->right,new_code);
               }
 }
 free(root);
 return;
}
void writedict(int total_entries)
{
 FILE *fp = fopen("dictionary","wb");
 fwrite(codes,sizeof(struct code_list) * total_entries, sizeof(struct code_list) *         total_entries, fp);
 fclose(fp);
}                  
main()
{
  long int char_count[256];
  int max_index;
  max_index = readfile("2.jpg",char_count);
  node *root_node;
  root_node = genhufftree(char_count, max_index);
  codes = (struct code_list *)calloc(256,sizeof(struct code_list));         
  generatedict(root_node,"");
  writedict(max_index+1);
  getchar();
}
Note : please keep any file named "2.jpg" in the same folder as the  executable

我的猜测是,您的程序破坏了您调用malloc的早期位置之一malloc池(有很多malloc调用!您没有检查大多数 malloc 调用的返回值。

我建议在所有地方检查 malloc 的返回值,看看哪个先失败。此外,使用 valgrind (Linux( 或 purify (Windows( 检查代码中的内存泄漏。

如果你认为你的程序没有耗尽内存,那么很可能malloc()只是认为你已经耗尽了内存。如果释放未分配的指针、多次释放同一指针、覆盖缓冲区等,则可能会发生这种情况。仔细调试程序,查找这些问题,和/或使用valgrind自动查找它们。

相关内容

  • 没有找到相关文章

最新更新