I was working on a larger program and memcpy was causing it to crash. I replicated the situation in a small program and it does the same thing. I noticed that for some reason this program runs fine
// Runs fine
#include <iostream>
int main() {
char* s1 = "TEST"; // src
char* s2; // dest
memcpy(s2, s1, strlen(s1) + 1);
std::cout << s2 << std::endl; // Should print "TEST"
return 0;
}
But this program crashes
// Crashes
#include <iostream>
int main() {
char* s1 = "TEST"; // src
char* s2 = ""; // dest - Note the small change
memcpy(s2, s1, strlen(s1) + 1);
std::cout << s2 << std::endl; // Should print "TEST"
return 0;
}
I'm not sure why this is happening. Can someone please explain why it is crashing?
Thanks!