Malloc无法分配内存

时间:2012-05-21 13:55:20

标签: c memory-management 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 = '\0';
 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 memory\n");
               getchar();
               exit(1);
               }
               else 
               {
               strcpy(new_code,s);
               new_code[len] = '\0';
               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

2 个答案:

答案 0 :(得分:3)

我的猜测是你的程序在你调用malloc的那些早期地方之一中破坏了malloc池(有很多malloc调用!)。您没有检查大多数malloc调用的返回值。

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

答案 1 :(得分:1)

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