0

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?

10
  • 8
    Without seeing your code, it's hard to give you advice. Commented Aug 11, 2012 at 23:27
  • 2
    We can't do much without the code ^^ Commented Aug 11, 2012 at 23:28
  • Have you tried generating fewer strings or shorter strings? Commented Aug 11, 2012 at 23:37
  • erase() and clear() only remove content from the string, there is no guarantee that they deallocate the string memory. This is common for STL containers, since typical usage patterns show that deallocating a string buffer when removing content would be too much of a performance penalty. This other SO question might give you some useful advice. Commented Aug 11, 2012 at 23:38
  • 1
    I don't see any use of strings in the provided code snippet, only the writing of values into a character array. How were you able to call .erase() or .clear() on a (char *)? Commented Aug 12, 2012 at 0:18

1 Answer 1

0

I made some minimal changes to your code so it compiles and runs. The main problem was randString was declared as a single char when 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::string and shouldn't cause excessive memory use. If you still have a problem it's in code that you haven't shown.

#include <iostream> #include <cstdlib> 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; } int main() { // randString should be an array to hold 10 chars + null terminator char randString[11]; randStringMake(randString, 10); std::cout << randString << "\n"; } 
Sign up to request clarification or add additional context in comments.

19 Comments

thank you. that helps but do you know how to release any memory taken up by lots of strings?
I don't see you adding anything to the free store? Are you trying to remove memory off the stack?
Also, you spelled AlphaNumeric incorrectly (in the original post above). It's minor, but should this code ever be maintained by someone else, they may have a hard time finding variables that are not properly spelled.
I'm really sorry if I'm not making sense. Could someone explain to me why my program is taking up large amounts of memory (like 2 GB), if I'm not even allocating any manually to the strings? If i sound like an idiot, its because I'm definitely a newbie!
@WillBrickner: This small bit of code can't be the source of your memory consumption. Do you have a minimal example of code that reproduces the problem? Does your program appear to run correctly (aside from using 2GB) or does it crash? Does your program use malloc, new, or recursion?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.