linux C语言:*** glibc detected *** ./control: free():invalid pointer:

野性酷女 2023-06-19 04:24 47阅读 0赞

前言

今天遇到了一个问题,折腾好久才找到问题原因,永远不要理想化,各种困难都会浮现的,我们只需要不骄不躁,一定可以解决所有问题。

问题代码

我们先来看一下问题代码部分吧。

  1. static char *control_process(char *verb[],char *noun[],char *result)
  2. {
  3. int verbOpcode = 0; //verb Opcode index
  4. int nounOpcode = 0; //noun Opcode index
  5. char *Matchverb = (char *)malloc(40*sizeof(char));
  6. char *Matchnoun = (char *)malloc(40*sizeof(char));
  7. char *Playfile = (char *)malloc(sizeof(char)*40);
  8. int index1 = 0;
  9. printf("enter the control_process \n\n");
  10. for(index1 =1; index1 <37;index1++)
  11. {
  12. Matchverb = mystrstr1(result,verb[index1]);
  13. if(Matchverb != NULL)
  14. {
  15. verbOpcode = index1;
  16. break;
  17. }else
  18. {
  19. verbOpcode = 0;
  20. }
  21. }
  22. int index2 = 0;
  23. for(index2 = 1;index2 <29;index2++)
  24. {
  25. Matchnoun = mystrstr1(result,noun[index2]);
  26. if(Matchnoun != NULL)
  27. {
  28. nounOpcode = index2;
  29. break;
  30. }else
  31. {
  32. nounOpcode=0;
  33. }
  34. }
  35. printf("achieve the verb and noun deal \n\n");
  36. sprintf(Playfile,"/mnt/card/1%02d2%02d",verbOpcode,nounOpcode);
  37. printf("play file is %s \n\n",Playfile);
  38. free(Matchverb);
  39. free(Matchnoun);
  40. return Playfile;
  41. }

解决问题

就是这段代码,一直报错,初看确实看不到什么问题,为此我加了很多打印,最后确定问题就发生在 free(Matchverb);free(Matchnoun);这两句话,下面就讲一个一知识点,为什么free发生异常了呢?
我们在使用malloc分配内存后,指针变量获取到了地址。但是在程序中我们又给指针变量重新赋值了,因此地址发生了变化,此时我再次使用free()来释放,已经不是原来malloc分配的内存地址了,因此这就是指针地址非法。

修改方法

为了防止地址发生变化,我可以在定义两个指针变量保存malloc分配初地址,然后在释放之前将初地址重新赋值回去,这样就完美解决了。

  1. static char *control_process(char *verb[],char *noun[],char *result)
  2. {
  3. int verbOpcode = 0; //verb Opcode index
  4. int nounOpcode = 0; //noun Opcode index
  5. char *Matchverb = (char *)malloc(40*sizeof(char));
  6. char *Matchnoun = (char *)malloc(40*sizeof(char));
  7. char *Playfile = (char *)malloc(sizeof(char)*40);
  8. char *p1 = Matchverb;
  9. char *p2 = Matchnoun;
  10. int index1 = 0;
  11. printf("enter the control_process \n\n");
  12. for(index1 =1; index1 <37;index1++)
  13. {
  14. Matchverb = mystrstr1(result,verb[index1]);
  15. if(Matchverb != NULL)
  16. {
  17. verbOpcode = index1;
  18. break;
  19. }else
  20. {
  21. verbOpcode = 0;
  22. }
  23. }
  24. int index2 = 0;
  25. for(index2 = 1;index2 <29;index2++)
  26. {
  27. Matchnoun = mystrstr1(result,noun[index2]);
  28. if(Matchnoun != NULL)
  29. {
  30. nounOpcode = index2;
  31. break;
  32. }else
  33. {
  34. nounOpcode=0;
  35. }
  36. }
  37. printf("achieve the verb and noun deal \n\n");
  38. sprintf(Playfile,"/mnt/card/1%02d2%02d",verbOpcode,nounOpcode);
  39. printf("play file is %s \n\n",Playfile);
  40. Matchverb = p1;
  41. Matchnoun = p2;
  42. free(Matchverb);
  43. free(Matchnoun);
  44. return Playfile;
  45. }

完美解决问题,这是我想到的办法,如果你有更好的办法,欢迎讨论,互相学习!!!!

发表评论

表情:
评论列表 (有 0 条评论,47人围观)

还没有评论,来说两句吧...

相关阅读

    相关 [C++学习]指针(pointer)总结

    一、描述 指针是 “ 指向 ” 另外一种类型的复合类型。 与引用(reference)不同之处在于: 1. 指针本身就是一个对象,允许对指针进行复制和拷贝,而且在其

    相关 C++指针(pointer

    C++指针(pointer)   在计算机科学中,指针(Pointer),是编程语言中的一类数据类型及其对象或变量,用来表示或存储一个存储器地址,这个地址的值直接指向(