+ 5

Is there some limit to the number of letters we can enter in a string?

I tried using a string to store a very large number, around 35660 digits long (Its 10000!, what I wish to store...). Unfortunately, the program displayed a bad_alloc error, when I assigned this value to a string. So, is there a limit to the amount of characters we can store in a string? If there is, Why is it so small? I was only able to enter at max 240 characters per string. Isn't the basic_string class supposed to have a larger size than 240? Or was it the problem of getline(), which I used?

20th Jul 2017, 3:36 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
13 odpowiedzi
+ 5
I had a problem storing mine too but figured out that the string has to be on 1 line. That +100,000 characters had to be on 1 line. 😂 1 line, can you believe it. So try storing it in your code again but on 1 line. Go take also a look at my code's JS here. You'll definitely get an idea of how I got it right. https://code.sololearn.com/WLoLoVyZWd2R/?ref=app
20th Jul 2017, 4:27 PM
Ghauth Christians
Ghauth Christians - avatar
+ 7
in c# I'd suggest you to look on BigInteger class, maybe c++ has something like that..
20th Jul 2017, 3:40 PM
Illusive Man
Illusive Man - avatar
+ 5
@Gavin It is as I thought, writing the number does not return any error. This problem is solved, but I still have to find a way how to copy paste data on console easily, without getting that error... Thank You!
20th Jul 2017, 4:33 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
@Gavin Christians So, will this method, max_size, return the number of bytes or the number of characters? //Well it is the same for both. // I am getting 1073741820 as the answer, if that is what I assume it is, then I should be able to store my number...
20th Jul 2017, 3:47 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
@Gavin I am providing input to the PC in one line, but I think the error is due to the fact that I am copying the data from a file. After all, I can't simply go enter all the digits of 10000! factorial without copy or paste. Thus, maybe some unwanted chars came along, and the string crashed. All my recent inputs were copy pastes... Ill now try writing a large string, holding a number key for very long...
20th Jul 2017, 4:31 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Glad you solved it ☺
20th Jul 2017, 4:34 PM
Ghauth Christians
Ghauth Christians - avatar
+ 2
@Illusive Man Im actually trying to develop one of my own 😅. Looks like I can't trust strings for this...
20th Jul 2017, 3:42 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
Correct @Kinshuk Vasisht. My highest character count I had for a string was over 100,000 characters for my Wisdom Calculator Code
20th Jul 2017, 4:18 PM
Ghauth Christians
Ghauth Christians - avatar
+ 2
Then, why can't I store my number? Some problem with cin?
20th Jul 2017, 4:19 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
I tried both ways. Input is where I have the problem, it automatically shows std::bad_alloc after I hit enter... When I try assigning it in code, it fails as well, but reading from a file is successful...
20th Jul 2017, 4:23 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
Why did nobody mention these approaches: std::string str1; str1.reserve(size); std::vector<char> str2; Both give the desired effect because vector should resize before its mem is used. Using the reserve() member of string allows you to preallocate memory on the other hand and since you know how much you need, allocate enough. This is why I like C, I know after any *alloc call whether I have enough memory.
21st Jul 2017, 9:04 AM
Jamie
Jamie - avatar
20th Jul 2017, 3:43 PM
Ghauth Christians
Ghauth Christians - avatar
+ 1
How are you storing it? Directly in your code or as an input you have to enter?
20th Jul 2017, 4:21 PM
Ghauth Christians
Ghauth Christians - avatar