I am trying to wirte a simple function to move the memory.That sounds like a easy job. However a corruption occur AT LINE 15 when I run the test code in VS 2013 in C language. Thanks a lot for yuor time. :)
Here is source code:
#include <stdio.h>
void* mMemmove(void* dest, const void* src, size_t n)
{
char* mdest = dest;
const char* msrc = src;
if (msrc < mdest)
{
msrc += n;
mdest += n;
while (n-- != 0)
{
*mdest = *msrc; // where exception occurs !!
--mdest;
--msrc;
// It was not this function having bug, but the
//variabe I declare in the main function that cause
//the exception.
}
}
else
{
while (n-- != 0)
{
*mdest = *msrc;
++mdest;
++msrc;
}
}
return mdest;
}
int main(int argc, const char* argv[])
{
//char* str1 = "asdfghjkl";
//char* str2 = "as"; BUG!
char str1[] = "asdfghjkl";
char str2[] = "as";
mMemmove(str2, str1, 5);
puts(str1);
putchar('\n');
puts(str2);
return 0;
}
the result in VS2013 and win10 shown at the link following
https://i.stack.imgur.com/LIp6h.png
update: why I write this if brach