Hi all
I'm using a 'STRING' variable to store the entire contents of a file. Is there any possibility of buffer overflow if the content of the file is large? (eg: say size of the file is 30MB).
Thanks in advance

Recommended Answers

All 3 Replies

Previous: strings can only hold a certain amount of data, so I'd assume so.

Have you considered using the non-standard rope to meet your data needs? Or possibly a vector<char> implementation?

Edit: Actually my previous post might need some more investigation, so I'm omitting it. My apologies.

Yes, there is a maximum size of a std::string object. Click here. The output of the example code in the URL is this:

size: 11
length: 11
capacity: 15
max_size: 4,294,967,294
Press any key to continue . . .

Yes, thank you AD for the clarification. I saw a developer mention a max size of the std::string class but didn't really investigate sources until recently. Here's some more information.

Edit: Depending on what a kilo-byte, etc means these days, you'll be in reasonable hands if you pull a 30 MB file into a string.

For example, if a megabyte is 1024 kilobytes and a kilobyte is 1024 bytes, and each char is a byte (which, a standard char should be) then you are looking at 30 * 1024 * 1024 bytes of information, which is only 251658240 bytes which is far under the limit of a string object.

If MB means 1000 kilobytes and a kilobytes is 1000 bytes, you're looking at a slightly smaller number than the one mentioned.

To be safe, assume 1024 for every additional "step" in bytes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.