I am making a program in C++ using Xcode that will generate random strings but when I do this it consumes an incredible amount of RAM. I have tried using .erase(); and .clear(); but neither seems to work.
here is my code:
void randStringMake(char *s, int l)
{
// AlphaNumaric characters
static const char AlphaNumaric[] = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "1234567890";
for(int x = 0; x < l; x++) {
s[x]=AlphaNumaric[rand() % (sizeof(AlphaNumaric) - 1)];
}
s[l] = 0;
}
char randString;
randStringMake(randString, 10);
std::cout << std::string(&randString) << "\n";
So i guess my question here, is how do i remove the string from the memory?
I made some minimal changes to your code so it compiles and runs. The main problem was
randStringwas declared as a singlecharwhen it should be an array large enough to hold the generated string and a null terminator.Note that the code as posted doesn’t required
std::stringand shouldn’t cause excessive memory use. If you still have a problem it’s in code that you haven’t shown.