Greetings all,
I am in the middle of testing a class I wrote to keep sorted strings together. However, I have run into a HUGE problem.
I have one function that inserts these strings into the classes array as a pointer:
Code:
void sortedStrings::insert(const char* entry)
{
data = new char[strlen(entry)+1];
strcpy(data,entry);
}
That is a simplified version, but it illustrates my problem.
Now the real fun begins, when entry passes into the insert function, the string is still intact (e.g. if entry = "hello", I can still print that), but after the first line where strlen() is run, if I print the value of entry I get random ASCII characters "!@@!&^as@#CV" <-- like that!
My theory is it is because strlen is trying to act on the pointer entry, and not the array of chars that entry points to, but I tried running strlen with *entry to dereference the pointer and it wouldn't compile (said couldn't convert a char to a char*...I THOUGHT IT ALREADY WAS A CHAR*!!!)
Please tell me what I am doing wrong so that strlen returns an actual length without destroying the value in entry (which you SHOULDN'T be able to change anyways since it is const!)
TIA