const char* findThisMsgInMem = "Hello World! Everyone is Happy";
if(memcmp(&dataInMem[i], findThisMsgInMem, defaultMsgSize) == 0)
{
memcpy(&dataInMem[i], newMsg, strlen(newMsg));
//more code here
}
This is first time for me trying to use malloc, and realloc. Please see the coding example for illustration purposes. Don't worry of this lengthy question, most of it is an explaination of the problem i'm facing.
Explain what going on:
memblock is of type char * .
dataInMem contains long stream data which includes this "Hello World! Everyone is Happy".
"Hello World! Everyone is Happy" that resides in dataInMem is of type const char*.
memcmp(&dataInMem, findThisMsgInMem, defaultMsgSize) is looking for "Hello World! Everyone is Happy" inside memcmp.
memcpy(&dataInMem, newMsg, strlen(newMsg)) copies/replaces the newMsg user entered into the place of "Hello World! Everyone is Happy". newMsg is of type const char*.
The expected result is:
"Hello World! Everyone is Happy" will be all overwritten if the length of newMsg equals the length the string "Hello World!.....". Otherwise, "Hello World! Everyone is Happy" inside dataInMem will be partially overwritten which produce a corrupted results. For example, if newMsg is "Don't Play with Memory" then the overwritten data in dataInMem will look like
Don't Play with MemoryHappy
Happy is not overwritten. Therefore, the solution is to go and take off the remained part (Happy) ONLY. I can solve such a problem when i'm dealing with strings, but in the current case most probably i will have to use memmove() or memcpy() to rid of "Happy". I thought to replace happy with spaces, but it is really not a good solution because putting spaces is not solving anything, space counted as char.
Anyhow, this is how i replaced Happy with spaces. I set newMsg as string, then i calculated the difference in length of the newMsg and defaultMsgSize. In our case, the remained length is 5 (Happy). After that, i appended spaces at the of newMsg, so newMsg has the word with five spaces and will be written in memory as "Don't Play with Memory " , which in someway not a proper solution.
I got other solutions, but they seems useless, because no matter what i do, i must solve this problem within dataInMem except if newMsg length equals defualtMsgSize. Therefore the proper solution as far as i see is to use malloc and realloc or anything related to alloc. I have no background on using malloc, etc yet.
dataInMem extracted data from a binary file (.exe). Thus, "Hello World! Everyone is Happy" already existed in the binary file. I can change the length of the string from "Hello World...." to
"X" before i compile the binary file (.exe), but it is expected that the newMsg will start from 1 to 15 (0 to 14) chars in length, so if i set the default message is "X" and newMsg "xxxxxxxxxxxxxxx" length is 15, then i'm facing the problem of overwriting other data that comes after X, which this corrupt the file totally, thats why i though about malloc and realloc. I got some questions about malloc and realloc too, but i will stop here by now.
I hope you got my point...
I need to hear your suggestions, tips, advices?