Hello, I'm writing a library using low level C++ methods so my library can easily be built in C. So I'm staying away from C++ standard templates. So vectors and strings are a bit limit. (Plus I think it's fun to have a bit of a challenge. ;) ). I've mostly been using strings in C++ for ease of use, and I decided to stick with char pointers, but I appear to run into odd problems, of which I cant explain.. I'll give a example.
//Issue One
char buffer[MAX_LENGTH];
sprintf_s(buffer, MAX_LENGTH, "Hello World!")
const char* bufferP = doLogic(buffer);
//bufferP = "Goodbye World!" after doLogic
//Some random function in my class that has nothing to do with bufferP at all.
doMoreMath();
//Wut? Now bufferP equals random garbage data?
//bufferP = "/////////tx///"
I know this problem can be fixed doing this:
const char* bufferP = doLogin();
char buffer_a[MAX_NAME_LENGTH];
memset(buffer_a, 0, MAX_NAME_LENGTH);
strncpy(buffer_a, bufferP, strlen(bufferP));
But why does this happen? This issue never seems to happen on any other pointers I've created, like objects. Is it necessary I do everything in char arrays? So I don't lose data randomly?
Thank you, Andrew.