>> static char buffer[1024];
You said that the string could be longer than 1kB ... You should probably provide more space for the buffer ...
>> if(!(p = strstr(str, orig)))
>> return str;
The function returns two semantically different pointers in different situations. In this case it returns a char*, as a copy of one of the arguments (this might point to anything, including a string literal). In the normal case, a pointer to a static buffer is returned. Be careful with this, as you're making assumptions about the function's usage that aren't necessarily true.
>> sizeof(replace_str(login, "$dlul", dlu))
Since replace_str returns a char*, this will be the same as sizeof(char*). On a 32bit platform for example, that would be 4. It does NOT give you the length of the string - for that you need strlen.
>> tempstr = malloc(sizeof(replace_str(
>> strcpy(tempstr,replace_str
Note that you're calling replace_str twice here with the same exact parameters ... That's a bit wasteful, especially since you're dealing with large strings.
>> login = realloc(login,sizeof(temps
Same comment about sizeof here ...
>> strcpy(login,tempstr);
>> strcat(login, logindata);
Does the login string have enough room to hold the concatenation of those two ?
>> But every single way i try i get segmentation fault or memory corruption later in program...
Could you show us the exact code you used, as well as the line where the segmentation fault occurs ?
Main Topics
Browse All Topics





by: evilrixPosted on 2009-06-02 at 00:33:51ID: 24524450
Change...
char * string1 = "something really long (about 1k of characters)"
char * string2 = "something as long or even longer"
to be
char string1[] = "something really long (about 1k of characters)"
char string2[] = "something as long or even longer"
The first form (apart from being deprecated in C++) is a pointer to a const literal in memory, the replacement form will create a copy of the const literal, which is mutable. Trying to perform a replace on a pointer to a const literal is almost certainly your issue.