【问题标题】:free(): invalid next size (normal) & munmap_chunk(): invalid pointer in C [duplicate]free():下一个大小无效(正常)和 munmap_chunk():C 中的无效指针 [重复]
【发布时间】:2020-08-29 21:51:39
【问题描述】:

一遍又一遍地调用函数enc()dec(),它会抛出错误,但会创建新的加密文件。

如果先调用enc()再使用加密文件,如果在dec()中使用则显示Error: munmap_chunk(): invalid pointer

如果调用 enc() 并且您终止程序,稍后运行它并在现在加密的文件上使用 dec(),它会显示 Error: free(): invalid next size (normal)

void enc()
{
    FILE *fp1, *fp2;
  char filename1[FILENAME_MAX];
  int i;
  int size;
  int shift;


  printf("Enter filename:");
  scanf(" %[^\n]s",filename1);

  while((fp1 = fopen(filename1, "r")) == NULL){
    fprintf(stderr, "Error: Unable to open file %s\nTry Again\n", filename1);
    printf("Enter filename:");
    scanf(" %[^\n]s",filename1);
  }


  printf("Enter shift:");
  scanf("%d",&shift);

  const char *prefix = "encrypted_";
  char *filename2;
  filename2 = (char *)malloc(sizeof(strlen(filename1) + strlen(prefix)+1)); 
  strcpy(filename2, prefix);
  strcat(filename2, filename1);
  if ((fp2 = fopen(filename2, "w")) == NULL) {
    fprintf(stderr, "Error: Unable to open file %s\n", filename1);
    exit(EXIT_FAILURE);
  }
  free(filename2);



  int ch;
  while ((ch = getc(fp1)) != EOF) {
    if (ch >= 'A' && ch <= 'Z') {
      putc(((ch - 'A') + shift) % 26 + 'A', fp2);
    } 
    else if (ch >= 'a' && ch <= 'z') {
      putc(((ch - 'a') + shift) % 26 + 'a', fp2);
    } 
    else {
      putc(ch, fp2);
    }
  }
  fclose(fp1);
  fclose(fp2);
}

void dec()
{
    FILE *fp1, *fp2;
  char filename1[FILENAME_MAX];
  int i;
  int size;
  int shift;


  printf("Enter filename:");
  scanf(" %[^\n]s",filename1);


  while((fp1 = fopen(filename1, "r")) == NULL){
    fprintf(stderr, "Error: Unable to open file %s\nTry Again\n", filename1);
    printf("Enter filename:");
    scanf(" %[^\n]s",filename1);
  }

  printf("Enter shift:");
  scanf("%d",&shift);
  shift = 26-(shift%26);

  const char *prefix = "decrypted_";
  char *filename2;
  filename2 = (char *)malloc(sizeof(strlen(filename1) + strlen(prefix)+1)); 
  strcpy(filename2, prefix);
  strcat(filename2, filename1);
  if ((fp2 = fopen(filename2, "w")) == NULL) {
    fprintf(stderr, "Error: Unable to open file %s\n", filename1);
    exit(EXIT_FAILURE);
  }
  free(filename2);



  int ch;
  while ((ch = getc(fp1)) != EOF) {
    if (ch >= 'A' && ch <= 'Z') {
      putc(((ch - 'A') + shift) % 26 + 'A', fp2);
    } 
    else if (ch >= 'a' && ch <= 'z') {
      putc(((ch - 'a') + shift) % 26 + 'a', fp2);
    } 
    else {
      putc(ch, fp2);
    }
  }
  fclose(fp1);
  fclose(fp2);
}

【问题讨论】:

  • 那是很多代码。你能给我们一个线索吗?哪一行代码导致了错误?如果您希望我们将其放入 IDE 并为您调试,您能否至少给我们一些示例输入和输出?
  • 这对我来说很新鲜,所以我会尽可能详细地介绍它。完整代码链接:repl.it/@AlcorInf/Project19 2 个错误:1)当您选择选项 3,通过文件加密,给文件名 text.txt,它返回 encrypted_text.txt。使用相同的 encrypted_text.txt 使用选项 4 对其进行解密,它显示错误。但是,有时它会正确输出 2)加密一次后,它会给出 encrypted_text.txt 文件。终止 pgm 并再次运行它,解密它会引发另一个错误

标签: c++ c pointers gdb dynamic-memory-allocation


【解决方案1】:
filename2 = (char *)malloc(sizeof(strlen(filename1) + strlen(prefix)+1)); 

您正在为sizeof(size_t) 保留空间并可能调用malloc(8),删除sizeof 关键字:

filename2 = malloc(strlen(filename1) + strlen(prefix)+1); 

【讨论】:

  • 非常感谢!! @大卫拉涅利
  • 在 Linux(显然正在运行 OP)上,使用 Valgrind 或 Address Sanitizer 很容易发现这个(以及类似的堆损坏错误)。
猜你喜欢
  • 1970-01-01
  • 2018-08-23
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 2020-08-04
  • 1970-01-01
  • 2016-03-16
相关资源
最近更新 更多