+ 1

What will be string.length return if we assign a big value to a string. i.e greater than max value of int.

What will be string.length return if we assign a big value to a string. i.e greater than max value of int. string str = "bsbsbsns................." ; //large string whose length is actually greater than max length of int. What will str.length return? First of all, whether string will accept string value bigger than Max int value? If yes, then what will str.length?

16th Feb 2018, 4:26 PM
Shrawan Kumar Chavan
5 Réponses
+ 2
No it won't Kanaan, a well written class will always keep its data correct, or give an error. It will not simply let a variable overflow and see what happens. Remember that worldwide millions of programmers are using these standard functions. When so many people use a small piece of code, everything has been tried and stupid bugs have been found.
17th Feb 2018, 8:41 AM
Freddy
+ 1
I'd say that depends on the language you use, and thus what string implementation you have. A well designed class shouldn't be able to be tricked like that. Standard library functions are usually very well designed. My guess is that the length of your string is limited to the maximum value the size variable can hold. If I have time tonight I'll write a simple test.
16th Feb 2018, 4:38 PM
Freddy
+ 1
I tested it in C++ on a 32 bit Linux platform (on my 64 bit Mac the max size is 2^64, which will definitely not fit in my memory). In SoloLearn the code will give: "Memory limit exceeded". https://code.sololearn.com/cmXIS1396qnd/#cpp Anyway: the output on my Linux console is: Return type of string.length() is size_t, which is: 4 bytes big As size_t is unsigned, this gives a max of: 4294967295 characters output is now: 100000000 bytes big output is now: 200000000 bytes big output is now: 300000000 bytes big output is now: 400000000 bytes big output is now: 500000000 bytes big output is now: 600000000 bytes big output is now: 700000000 bytes big output is now: 800000000 bytes big output is now: 900000000 bytes big output is now: 1000000000 bytes big terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_create Aborted (core dumped) When looking through the std::string code I find: if (__capacity > _S_max_size) __throw_length_error(__N("basic_string::_S_create")); When looking a bit further I find: // The maximum number of individual char_type elements of an // individual string is determined by _S_max_size. This is the // value that will be returned by max_size(). (Whereas npos // is the maximum number of bytes the allocator can allocate.) // If one was to divvy up the theoretical largest size string, // with a terminating character and m _CharT elements, it'd // look like this: // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT) // Solving for m: // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1 // In addition, this implementation quarters this amount. So there appears to be a max_size() methond on std::string, when I call that I get: 1.073.741.820 bytes. That is a lot less than the theoretical max given by the size_t data type. On my Mac, max_size() returns: 18.446.744.073.709.551.599, which appears to be enough :) Nice question!
16th Feb 2018, 6:31 PM
Freddy
+ 1
oh didn't know that, cool cause I've never faced with it so i guessed it eould act like that cause that's a pretty normal act in programmin. well thanks for your great info @freddy 😊
17th Feb 2018, 9:05 AM
kanaan
kanaan - avatar
0
Thanks for the reply Freddy. Please share the results after you test it.
16th Feb 2018, 4:42 PM
Shrawan Kumar Chavan