I'm not used to getting this deep in c++, but all these pointers are making my brain boil. I'm trying to get a str_replace working

VOID str_replace(PCHAR Dest, PCHAR Find, PCHAR Replace) {
    PCHAR New=(char *)malloc(sizeof(CHAR)*MAX_STRING);
    PCHAR Found = Dest;
    PCHAR Temp = Dest;
    strcpy(New,"");
    while(strstr(Temp,Find))
    {
        Found = strstr(Temp,Find);
        strncpy(&New[strlen(New)], Temp, Found - Temp);
        strcat(New,Replace);
        Temp = Found + strlen(Find);
    }
    strcat(New,Temp);
    strcpy(Dest,New);
}

CHAR is just a char, PCHAR is just a pointer to a char, and MAX_STRING is 2048. i've tried a dozen diff things and I thought I finally had it with this routine above but it crashed on "while(strstr(Temp,Find))" after it had been running for about a minute, probably several thousand executions of the routine.

I know it's vague but can anyone point me in the right direction or explain what the hell i'm doing wrong. Do I need to destroy the memory at "New"?

at line 4, Found and Temp both point to the same string. Therefore the parameters to strstr() on line 6 are the same string. And line 9 will do nothing because Temp - Found is the same as Dest - Dest, which is 0.

sorry, its my similar naming. Line 6 is the passed parameter "Find", line 3 is the pointer "Found" for where the first "Find" is found in Dest/Temp. I don't remember why I pointed Found at Dest, I think it was from a previous experiment. The code works as far as it actually performing a replace, but it eventually leads to a crash =(

I think I need to free(New) at the end... i'll give it a shot and see =)

Hmmm I added free(New) to the end of the routine but im getting weird output now. Risidual text from previous runs is showing up in later runs for example

Bleh bloh bleh, replace bloh with blow, I get
Bleh blow bleh
Blab blab, replace ab with ob, I get
Blob blobw bleh

My mind is fried for the night :)

Edited 5 Years Ago by headedtomexico: n/a

Ok, latest run, i removed that pointer to

VOID str_replace(PCHAR Dest, PCHAR Find, PCHAR Replace) {
    PCHAR New=(PCHAR)malloc(sizeof(CHAR)*MAX_STRING);
    PCHAR Found;
    PCHAR Temp = Dest;
    strcpy(New,"");
    while(strstr(Temp,Find))
    {
        Found = strstr(Temp,Find);
        strncpy(&New[strlen(New)], Temp, Found - Temp);
        strcat(New,Replace);
        Temp = Found + strlen(Find);
    }
    strcat(New,Temp);
    strcpy(Dest,New);
    free(New);
}

I don't have the exact output with me but running something like this:

CHAR Output[MAX_STRING];
CHAR AnotherOutput[MAX_STRING];
strcpy(Output, "this test was a success");
str_replace(Output,"test","success");
WriteOutput(Output);
strcpy(AnotherOutput, "blahblah");
str_replace(AnotherOutput,"test","success");
WriteOutput(AnotherOutput);

id get something similar to the following:
this success was a success
blahblah s was a success


I ran this past a coworker today and he said it sounded like it was reallocating the same chunk of memory for "New" each time it ran and old information was still left there and corrupting the subsequent runs. He said I should add:
New = "\n";
right after i malloc, but i'm not seeing how that would fix the problem. It seems like I should do something like
memset(New,0,MAX_STRING);
or
memset(New,'\0',MAX_STRING);


anyone want care to tell me what stupid thing i've done =)

Ok, I think I may have solved my own problem... maybe, I can't run it till this afternoon.

What I think is happening is malloc is handing out the same chunk of memory sometimes, and the old string is still in there. Then strncpy pastes over the first part of that old string but strncpy doesnt NULL terminate. Then when I do the strcat after that, it adds the string to the risidual string left in the memory at "New" since strncpy didn't put a new NULL char in there.

If thats the case tonight when I run it i'll come update this.

Thanks everyone!

This question has already been answered. Start a new discussion instead.