0

Is there any problems in my code ,or to be improved ?

#include <stdio.h>
#include <stdlib.h>

char * filterdupchar(char* s,int n){
    char *tmp2 = s ;
    char *tmp =(char*)malloc(n*sizeof(char));
    for(int j=0;j<n/2;j++){
        int i=0,p=0;
        while(i<n){
            *(tmp+p) = s[i];
            if(s[i]==s[i+1])
                i = i+2;
            else
                i++;
            p++;
        }
    s = tmp;
   }
    s = tmp2;
    return tmp;
}
int main(){
     char * str= "bbaaan";
     printf("%s",str);
     printf("\n");
     char * strnew;
     strnew = filterdupchar(str,6);
     printf("%s",strnew);
     return 1;
}

The result should be "ban", but it is appended to something disordered character. In my func , is it necessary to give tmp2 to s, and do i need to free sth.?

4

1 回答 1

2

变化

  • 只需要一个循环和滞后字符来做过滤器(上面是不必要的复杂)
  • 需要为 tmp.. 分配字符数 + 1(对于 nullchar )
  • 不需要 malloc 强制转换
  • 将 nullchar 添加到 tmp 的末尾
  • main 应该在成功时返回 0。否则消耗它的进程会感到困惑
  • 需要免费的strnew..
  • 如果您有任何记忆疑问,请使用 valgrind
  • 连续调用打印是多余的..

代码

#include <stdio.h>
#include <stdlib.h>

char * filterdupchar(char* s,int n)
{
  int from,to; char last;
  char *tmp = malloc((n+1)*sizeof(char));
  from = to = 0;
  last = 0;
  for(from = 0; from < n; from++)
  {
    if(s[from] != last)
    {
      tmp[to] = s[from];
      to++;
    }
    last = s[from];
  }
  tmp[to] = '\0';
  return tmp;
}
int main()
{
  char * str= "bbaaan";
  printf("%s\n",str);
  char * strnew;
  strnew = filterdupchar(str,6);
  printf("%s\n",strnew);
  free(strnew);
  return 0;
}

输出

$ valgrind ./test
==11346== Memcheck, a memory error detector
==11346== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==11346== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==11346== Command: ./test
==11346== 
bbaaan
ban
==11346== 
==11346== HEAP SUMMARY:
==11346==     in use at exit: 0 bytes in 0 blocks
==11346==   total heap usage: 1 allocs, 1 frees, 7 bytes allocated
==11346== 
==11346== All heap blocks were freed -- no leaks are possible
==11346== 
==11346== For counts of detected and suppressed errors, rerun with: -v

笔记

始终使用内存检查器(如 valgrind)来测试内存泄漏

于 2015-08-31T04:34:40.770 回答