Click here to Skip to main content
15,889,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to store large strings in c/c++ having size nearly 10^6 characters
Posted
Updated 23-Jun-13 2:15am
v2
Comments
Richard MacCutchan 23-Jun-13 8:16am    
What sort of string would have that many characters? The chances are that you sould consider breaking the data up into more manageable chunks for ease of processing.

If you know the exact size in advance and it won't change, just use new. You should be able to allocate a thousand times as much without a problem.

If you don't know the exact size in advance, or the string contents (and size) will change, then in theory you should use the class std::string instead. It will take care of (re)allocations, if needed. That said, any change will take considerably more time for a single large string, than it would take if you created a properly structured set of smaller strings instead. If for instance the string holds the contents of a text file, it would be more sensible to store each line inside a separate string variable.
 
Share this answer
 
Comments
nv3 24-Jun-13 6:36am    
My 5!
Just allocate buffers with sufficient size using new (C++) or malloc (C).
 
Share this answer
 
Comments
JackDingler 24-Jun-13 8:43am    
That's only a megabyte. So this solution is fine, as is using std::string as mentioned by others.

IF you have some big hardware and need many of these strings, then go 64bit.
I would look at the Standard Template Library std::string


http://www.cprogramming.com/tutorial/string.html[^]
 
Share this answer
 
I would consider using a file to store the data and read it in small chunks for processing.
If you're on Windows, I would recommend, storing the data in a file and memory mapping the file using the following APIs -
CreateFile
CreateFileMapping
MapViewOfFile
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900