尝试写入文件时,malloc断言失败

时间:2018-04-08 18:43:56

标签: c

上下文:我在C中制作一个Huffman压缩脚本。虽然效率不高,但我必须将头写入压缩文件,以便稍后我可以解压缩该文件。

这是标题的样子(纯文本):

e=01010;

这是为我的霍夫曼树中的每个角色写入标题的函数:

void write_header(struct node_m *node, FILE *stream)
{
  unsigned char buffer[get_len(node) + 3];
  buffer[0] = get_value(node);
  buffer[1] = '=';

  int i = 2;
  unsigned char *coding = get_coding(node);

  while (coding[i - 2])
  {
    buffer[i] = coding[i - 2];
    i++;
  }
  buffer[i] = ';';
  fwrite(buffer, sizeof(unsigned char), i + 1, stream);
}

struct node_m包含对我的函数至关重要的这两个字段:valueunsigned char,其中包含节点所代表的特征,而coding是一个unsigned char字符串,表示此格式的编码:01010

使用GDB运行该功能:

331   fwrite(buffer, sizeof(unsigned char), i + 1, stream);
(gdb) p buffer
$1 = "h=00000;"
(gdb) p sizeof(unsigned char)
$2 = 1
(gdb) p i+1
$3 = 8
(gdb) p stream
$4 = (FILE *) 0x0
(gdb) p get_len(node)
$5 = 5
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
__GI__IO_fwrite (buf=0x7fffffffce30, size=1, count=8, fp=0x0) at iofwrite.c:37
37  iofwrite.c: No such file or directory.
(gdb) n

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.

在我的终端上,当运行整个事情时,它并没有告诉我我有分段错误,而是这个:

compresse: malloc.c:2394: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Makefile:73: recipe for target 'compresse' failed
make: *** [compresse] Aborted (core dumped)

compresse是我的主文件,其中包含main()

我很遗憾我在这里做错了什么。

0 个答案:

没有答案