My Replace function Works while debugging But not compiled file doesnot

Discussion in 'C' started by _username, May 28, 2008.

  1. _username

    _username New Member

    Joined:
    May 28, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I made a Replace fuction that works while I debug (Vs 2005 ) the compiled file closes due to some some :( any help would be appreciated


    this is my code
    Code:
    char* _replace(char* source_str,char* search_str,char* replace_str,char* ostr)
    {
    char *data = source_str;
    char *orgdata = data;
    int ser_sz = strlen(search_str);
    int rep_sz = strlen(replace_str);
    char _k = 0;
    ostr = malloc(strlen(source_str));
    ostr[0] = '\0';		/* remove garbage value */
    if (rep_sz >= ser_sz){ _k = 1; } 
    do{
      char *p = strstr(data,search_str);	/* get the pointer to the first occurrence */
    	if (!p){ 
             strcat(ostr,orgdata);	/*append the rest*/
             break;
    	}
    	if (!_k) {
    		realloc(ostr,(strlen(ostr) + rep_sz-ser_sz));
    	}
      strncat(ostr,orgdata,p - orgdata);	/*append the rest of the string*/
      data = p + ser_sz;	/*ignore the search term*/
      strncat(ostr,replace_str,rep_sz);	/* place the replace string*/
      orgdata = p + ser_sz;	/* save the current data*/
      	}while(1);
       
    return ostr;
    }
     
  2. _username

    _username New Member

    Joined:
    May 28, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Okay ! i found a solution but its really awkward

    I found that If I print some thing before I call the fuction it works good FOr example

    Code:
     char *buffer = 0;
     char *strr = "LLkLLkbsbsbsbsbs";
     printf("_usename");
      buffer = _replace(strr,"LLk","LOLOLOLOLOLOLOLOLOLOLOLOLOLO :P",buffer);
    
     printf("%s",buffer);
     getchar();
    THis one works but the following one doesn't

    Code:
     char *buffer = 0;
     char *strr = "LLkLLkbsbsbsbsbs";
     /*printf("here");*/
      buffer = _replace(strr,"LLk","LOLOLOLOLOLOLOLOLOLOLOLOLOLO :P",buffer);
    
     printf("%s",buffer);
     getchar();
     
  3. _username

    _username New Member

    Joined:
    May 28, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I cannot find the Edit Sry ---

    SO can any one explain what I am doing wrong pls
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    Edit button will come after you get your post count into double figures.
     
  5. mr.anandc

    mr.anandc New Member

    Joined:
    May 6, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Usage of return value of _replace(assigning to dest) should work fine
    Code:
    int main()
    {
    char search[] = "Hi";
    char replace[] = "Bye";
    char str[] = "HelloHiHelloHi";
    char *dest;
    dest = _replace(str, search, replace, dest);
    printf("replaced string is %s\n", dest);
    return 0;
    }
    
    Output of this is - HelloByeHelloBye. But if we directly use dest like below
    Code:
    _replace(str, search, replace, dest);
    printf("replaced string is %s\n", dest);
    
    this may result into segmentation fault. This is because "dest" is not initialized(not pointing) to any memory location. "dest" is passed as a value(not by pointer) and _replace function allocates memory locally and initializes it to local pointer(ostr - and this does not reflect to dest). On exit of _replace function ostr variable is deallocated and we lost the reference to allocated memory. So, it could result into memory leak too.

    We should be careful when allocating memory in called function. It is good idea to always return the pointer to allocated memory. So the _replace specification becomes

    Code:
    char* _replace(char* source_str,char* search_str,char* replace_str) // last argument is removed
    
    But still if the last argument is required then pass address of "dest" to _replace. Then specification becomes

    Code:
    char* _replace(char* source_str,char* search_str,char* replace_str,char** ostr) // type of ostr is changed to char**
    
    and _replace function has to be updated accordingly(just replace ostr usage with *ostr) so that the change in ostr reflects to "dest" in callee function.

    Hope this may help you.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice