memcpy and memcmp Question...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
		if(memcmp(&memblock[i], DEFAULT_IP.c_str(), defaultIPSize) == 0)
		{
			memcpy(&memblock[i], newIPAddress, strlen(newIPAddress));
			int count = 0;
			while (count < unwantedDataSize_IP)
			{
				i++;
                                //i get error that says error C2106: '=' : left operand must be l-value
				&memblock[i] = '\0';
                                count++;
			}

                        //more code here
                }


I got fed up. I could not solve this! My problem is:

memblock contains alot of stream data. I need to exchange a piece of data with another one. Once memcmp find DEFAULT_IP.c_str() inside memblock, we will do memcpy, we need to copy newIPAddress in place of DEFAULT_IP.c_str()...Ok

till now the code work fine. BUT if the size of newIPAddress less than the size of DEFAULT_IP then after the exchange happens, some data of the DEFAULT_IP will remain after newIPAddress, which i do not want that! So i used another short while loop to go after the extra remained data & wipe them out, i did &memblock[i] = '\0'; but i get an error.

I can solve this problem with string by using erase, but i must do it within memory...

memblock is of type char *
newIPAddress is of type const char *
DEFAULT_IP is of type string

I hope my question is clear. Can you help? I tried several solutions, but unfortunately did not work cause finally i discovered the whole thing should be done in memory memblock directly.

For example,

default ip is 255.255.255.255
new ip is 44.233.44.22
The result will be 44.233.44.22255 (note the 255 at the end)

255.255.255.255 already resides in memblock, so the only way to eliminate the extra 255, is to go through a loop and do something such as delete, remove, wipe, anything relates to rid of...unwantedDataSize_IP is the number of unwanted data that we need to remove...

Any idea?
Last edited on
Why are you not just using string's member functions to manipulate the data?

I'll try this

1
2
3
4
5
6
7
			while (unwantedDataSize_IP > 0)
			{
				const char *brk = '\0';
				i++;
				memcpy(&memblock[i], brk, unwantedDataSize_IP);
				//&memblock[i] = '\0';
			}
Solved in a completely different way. I wish i just did it instead of wasting my time. Yes i believe premature optimization is the root of all evil
Last edited on
Topic archived. No new replies allowed.