Memory C和C+中的内存泄漏+;代码

Memory C和C+中的内存泄漏+;代码,memory,memory-leaks,Memory,Memory Leaks,我试图从一个函数返回一个指针,并在另一个函数中使用该返回,但内存泄漏。 我编写的测试代码,通过CPPCheck检测内存泄漏 ######################################################################## # include < stdio.h > # include < malloc.h > # include < string.h > char* replace ( char* st, c

我试图从一个函数返回一个指针,并在另一个函数中使用该返回,但内存泄漏。
我编写的测试代码,通过CPPCheck检测内存泄漏

########################################################################

# include < stdio.h >
# include < malloc.h >
# include < string.h >

char* replace ( char* st, char* word, char *replaceWith );
int main ( void ) 
{
    char str[] = "Hello how are ## and what are ## doing ?";
    char word[]="##";
    char replaceWith[]="you";
    printf("%s",replace(str,word,replaceWith));
    getchar();
    return 0;
}

char* replace(char* st,char* word,char *replaceWith)
{
 int i = 0;
 char *sr,*s,*ret;
 int oldlen;
 int count = 0;
 int newlen;
 int stlen;
 s=(char *)malloc(strlen(st) + 1);
 strcpy(s, st);
 oldlen=strlen(word);
 newlen=strlen(replaceWith);
 for (i = 0; s[i]! = '\0'; )
 {
     if( memcmp( &s[i], word, oldlen ) == 0)
     {
        count++;
        i+=oldlen;
     }

     else
     {
         i++;
     }
 }
 sr= (char *) malloc (i+1+count*(newlen-oldlen));
 ret = (char *) malloc (i+1+count*(newlen-oldlen));
 ret=sr;
 while(*s)
 {
     if(memcmp( s, word, oldlen) == 0)
     {
         memcpy(sr, replaceWith, newlen);
         s+ = oldlen;
         sr+ = newlen;
     }
     else
     {
         *sr++ = *s++;
     }
 }

 *sr = '\0';
 return ret;
}
########################################################################
#包括
#包括
#包括
char*replace(char*st,char*word,char*replaceWith);
内部主(空)
{
char str[]=“你好,你好,你在做什么?”;
字符单词[]=“###”;
字符替换为[]=“您”;
printf(“%s”,替换为(str,word,replacetwith));
getchar();
返回0;
}
char*replace(char*st,char*word,char*replaceWith)
{
int i=0;
字符*sr、*s、*ret;
国际奥德伦;
整数计数=0;
内纽伦;
int stlen;
s=(char*)malloc(strlen(st)+1);
strcpy(s,st);
奥尔德伦=斯特伦(字);
newlen=strlen(替换为);
对于(i=0;s[i]!='\0';)
{
if(memcmp&s[i],word,oldlen)==0)
{
计数++;
i+=oldlen;
}
其他的
{
i++;
}
}
sr=(char*)malloc(i+1+count*(newlen-oldlen));
ret=(char*)malloc(i+1+count*(newlen-oldlen));
ret=sr;
而(*s)
{
if(memcmp(s,word,oldlen)==0)
{
memcpy(sr、replaceWith、newlen);
s+=奥尔德伦;
sr+=newlen;
}
其他的
{
*sr++=*s++;
}
}
*sr='\0';
返回ret;
}

始终使用malloc释放相同的计数

  • 更换结束时的自由s、sr、
  • 使用replace的返回值,而不是在printf上直接使用
    以及不需要时的自由返回值(ret从replace返回)
    始终使用malloc释放相同的计数

  • 更换结束时的自由s、sr、
  • 使用replace的返回值,而不是在printf上直接使用
    以及不需要时的自由返回值(ret从replace返回) 试试这个

    #include<stdio.h>
    #include<malloc.h>
    #include<string.h>
    
    char* replace ( char* st, char* word, char *replaceWith );
    
    int main ( void ) 
    
    {
    
        char str[] = "Hello how are ## and what are ## doing ?";
    
        char word[]="##";
    
        char replaceWith[]="you";
    
        char * ret = replace(str,word,replaceWith);
        printf("%s",ret);
        free(ret); //freeing the allocated memory 
        getchar();
    
        return 0;
    }
    
    char* replace(char* st,char* word,char *replaceWith)
    {
    
     int i = 0;
    
     char *sr,*s,*ret, *temps;
    
     int oldlen;
    
     int count = 0;
    
     int newlen;
    
     int stlen;
    
     s=(char *)malloc(strlen(st) + 1);
     temps = s; // storing the address of s in a temp location
     strcpy(s, st);
    
     oldlen=strlen(word);
    
     newlen=strlen(replaceWith);
    
     for (i = 0; s[i]!= '\0';)
    
     {
    
         if( memcmp( &s[i], word, oldlen ) == 0)
         {
    
            count++;
    
            i+=oldlen;
    
         }
    
         else
         {
             i++;
         }
    
     }
     sr= (char *) malloc (i+1+count*(newlen-oldlen));
    
     ret=sr;
    
     while(*s)
     {
         if(memcmp( s, word, oldlen) == 0)
         {
             memcpy(sr, replaceWith, newlen);
    
             s += oldlen;
    
             sr += newlen;
    
         }
         else
         {
    
             *sr++ = *s++;
    
         }
     }
    
     *sr = '\0';
     free(temps); // freeing the memory allocated for s
     return ret;
    
    }
    
    #包括
    #包括
    #包括
    char*replace(char*st,char*word,char*replaceWith);
    内部主(空)
    {
    char str[]=“你好,你好,你在做什么?”;
    字符单词[]=“###”;
    字符替换为[]=“您”;
    char*ret=replace(str、word、replaceWith);
    printf(“%s”,ret);
    释放(ret);//释放分配的内存
    getchar();
    返回0;
    }
    char*replace(char*st,char*word,char*replaceWith)
    {
    int i=0;
    字符*sr、*s、*ret、*TEMP;
    国际奥德伦;
    整数计数=0;
    内纽伦;
    int stlen;
    s=(char*)malloc(strlen(st)+1);
    temps=s;//将s的地址存储在临时位置
    strcpy(s,st);
    奥尔德伦=斯特伦(字);
    newlen=strlen(替换为);
    对于(i=0;s[i]!='\0';)
    {
    if(memcmp&s[i],word,oldlen)==0)
    {
    计数++;
    i+=oldlen;
    }
    其他的
    {
    i++;
    }
    }
    sr=(char*)malloc(i+1+count*(newlen-oldlen));
    ret=sr;
    而(*s)
    {
    if(memcmp(s,word,oldlen)==0)
    {
    memcpy(sr、replaceWith、newlen);
    s+=奥尔德伦;
    sr+=newlen;
    }
    其他的
    {
    *sr++=*s++;
    }
    }
    *sr='\0';
    释放(temps);//释放为s分配的内存
    返回ret;
    }
    
    试试这个

    #include<stdio.h>
    #include<malloc.h>
    #include<string.h>
    
    char* replace ( char* st, char* word, char *replaceWith );
    
    int main ( void ) 
    
    {
    
        char str[] = "Hello how are ## and what are ## doing ?";
    
        char word[]="##";
    
        char replaceWith[]="you";
    
        char * ret = replace(str,word,replaceWith);
        printf("%s",ret);
        free(ret); //freeing the allocated memory 
        getchar();
    
        return 0;
    }
    
    char* replace(char* st,char* word,char *replaceWith)
    {
    
     int i = 0;
    
     char *sr,*s,*ret, *temps;
    
     int oldlen;
    
     int count = 0;
    
     int newlen;
    
     int stlen;
    
     s=(char *)malloc(strlen(st) + 1);
     temps = s; // storing the address of s in a temp location
     strcpy(s, st);
    
     oldlen=strlen(word);
    
     newlen=strlen(replaceWith);
    
     for (i = 0; s[i]!= '\0';)
    
     {
    
         if( memcmp( &s[i], word, oldlen ) == 0)
         {
    
            count++;
    
            i+=oldlen;
    
         }
    
         else
         {
             i++;
         }
    
     }
     sr= (char *) malloc (i+1+count*(newlen-oldlen));
    
     ret=sr;
    
     while(*s)
     {
         if(memcmp( s, word, oldlen) == 0)
         {
             memcpy(sr, replaceWith, newlen);
    
             s += oldlen;
    
             sr += newlen;
    
         }
         else
         {
    
             *sr++ = *s++;
    
         }
     }
    
     *sr = '\0';
     free(temps); // freeing the memory allocated for s
     return ret;
    
    }
    
    #包括
    #包括
    #包括
    char*replace(char*st,char*word,char*replaceWith);
    内部主(空)
    {
    char str[]=“你好,你好,你在做什么?”;
    字符单词[]=“###”;
    字符替换为[]=“您”;
    char*ret=replace(str、word、replaceWith);
    printf(“%s”,ret);
    释放(ret);//释放分配的内存
    getchar();
    返回0;
    }
    char*replace(char*st,char*word,char*replaceWith)
    {
    int i=0;
    字符*sr、*s、*ret、*TEMP;
    国际奥德伦;
    整数计数=0;
    内纽伦;
    int stlen;
    s=(char*)malloc(strlen(st)+1);
    temps=s;//将s的地址存储在临时位置
    strcpy(s,st);
    奥尔德伦=斯特伦(字);
    newlen=strlen(替换为);
    对于(i=0;s[i]!='\0';)
    {
    if(memcmp&s[i],word,oldlen)==0)
    {
    计数++;
    i+=oldlen;
    }
    其他的
    {
    i++;
    }
    }
    sr=(char*)malloc(i+1+count*(newlen-oldlen));
    ret=sr;
    而(*s)
    {
    if(memcmp(s,word,oldlen)==0)
    {
    memcpy(sr、replaceWith、newlen);
    s+=奥尔德伦;
    sr+=newlen;
    }
    其他的
    {
    *sr++=*s++;
    }
    }
    *sr='\0';
    释放(temps);//释放为s分配的内存
    返回ret;
    }
    
    我对内存泄漏做了很多实验,同时我编写了以下代码。请评论一下它的利弊

    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    
    // Prototype declaration of replaceAll function
    static char* replaceAll(char *pSource, char *pWord, char*pWith);
    
    
    /////////////////////////////////////////////////////////////////////////////
    //
    //   NAME           :     main
    //
    //   DESCRIPTION    :     Implementation of main which invokes the replaceAll 
    //                        function and displays the output             
    //
    //   PARAMETERS     :     void 
    //
    //   RETURNED VALUE :     int
    //
    /////////////////////////////////////////////////////////////////////////////
    
    int main( void )
    {
        char *finalString = NULL;                           // To save the base returned address
        char srcString[] = "Hello how r you";               // Actual String 
        char pWord[] = "r";                                 // Word to be replaced
        char pWith[] = "are";                               // Word to be replaced with
    
        printf("\n Before Calling the replaceAll function:");
    
        printf("%s",srcString);
    
        printf("\n");
    
        finalString = replaceAll(srcString, pWord, pWith);  //calling the replaceAll function
    
        printf("\n After  Calling the replaceAll function:");
    
        // Checking if NULL is returned
        if( finalString != NULL )
        {
            //printing the string
    
            printf("%s", finalString);
    
        }
        else
        {
            printf("\n Error: Blank String returned ");
        }
    
        return 0;
    }
    
    /////////////////////////////////////////////////////////////////////////////
    //
    //   NAME           :     replaceAll
    //
    //   DESCRIPTION    :     Implementation of replaceAll function which replaces 
    //                        a word in given string with another word         
    //
    //   PARAMETERS     :     char * 
    //
    //   RETURNED VALUE :     char *
    //
    /////////////////////////////////////////////////////////////////////////////
    
    static char* replaceAll(char *pSource, char *pWord, char*pWith)
    {
    
        char *pSt = NULL;               // Pointer to the source String to avoid modifying the pSource 
        char *pTarget = NULL;           // Target pointer to be malloced
        char *pTg = NULL;               // Pointer to the target string
    
        int count;                      // Counter
        int nWord = strlen (pWord);     // length of the word which needs to be replaced
        int nWith = strlen (pWith);     // length of the word with which the word needs to be replaced
    
        static const char nullP = '\0'; // null character 
    
        int szTarget = 0;
    
        // Assigning the base address of the pSource to a temporary and iterate through
    
        for ( pSt = pSource, count = 0; *pSt != nullP; pSt++ )
        {
            // Count number of occurances of the Word in the String to calculate the length of the final string
            if( memcmp( pSt, pWord, nWord ) == 0)
            {
                count++;
                pSt += nWord-1;
            }
        }
    
        // Calculate the required target Size
        szTarget = strlen (pSource) + count * (nWith - nWord) + sizeof (nullP);
    
        // Allocate memory for the target string
    
        pTarget = (char *)malloc(szTarget);
    
        // Check if the malloc function returns sucessfully
    
        if ( pTarget != NULL)
        {
            // Copying the string with replacement
            for (pTg = pTarget, pSt = pSource; *pSt != nullP; )
            {
                if( memcmp (pSt, pWord, nWord) == 0)
                {
                    memcpy (pTg,pWith,nWith);
                    pSt += nWord; 
                    pTg += nWith;
                }
                else
                {
                    *pTg++ = *pSt++;
                }
            }
    
            // Assigning NULL Character to the target string after copying
            *pTg = '\0';
        }
        return pTarget;
    }
    
    #包括
    #包括
    #包括
    //replaceAll函数的原型声明
    静态char*replaceAll(char*pSource,char*pWord,char*pWith);
    /////////////////////////////////////////////////////////////////////////////
    //
    //姓名:main
    //
    //描述:调用replaceAll的main的实现
    //函数并显示输出
    //
    //参数:void
    //
    //返回值:int
    //
    /////////////////////////////////////////////////////////////////////////////
    内部主(空)
    {
    char*finalString=NULL;//保存基返回地址
    char srcString[]=“你好”;//实际字符串
    char pWord[]=“r”;//要替换的字
    字符pWith[]=“are”;//要替换为的单词
    printf(“\n在调用replaceAll函数之前:”);
    printf(“%s”,srcString);
    printf(“\n”);
    finalString=replaceAll(srcString,pWord,pWith);//调用replaceAll函数
    printf(“\n在调用replaceAll函数之后:”);
    //检查是否返回NULL
    if(finalString!=NULL)
    {
    //打印字符串
    printf(“%s”,最终字符串);
    }
    其他的
    {
    printf(“\n错误:返回空白字符串”);
    }
    返回0;
    }
    /////////////////////////////////////////////////////////////////////////////
    //
    //姓名:replaceAll
    //
    //说明: