+ 3
Unexpected error in C++ Code Playground...
I was trying to compile a code of mine, but for the first time, received the following as the output: """ The application has requested runtime to terminate it in an unusual way. Please contact the application support team for more information. what() : basic_string_S_create. """ I haven't even used try{} anywhere. And on CodeBlocks, the same code returns: ||=== Build finished: 0 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===| How am I to correct my code? Is this due to some error in my code or some Code playground Secret? Here is the code : https://code.sololearn.com/cTeO3C6896xL/?ref=app
11 odpowiedzi
+ 2
Yea, I notice the bug isn't there when I enter it on different lines, sometimes.
Nevertheless you should have checks in place for these types of things ( check out assert ).
And you should really take the strings as const string&, so you don't make copies.
Sure, 100000! factorial may have alot of digits, but you're not gonna need factorials every time you run the program, but still you waste the space for it.
You should allocate more whenever you need more.
+ 2
I'm not gonna read through the entire thing, but allocating 1000000, really?
Anyway, I placed
cerr << __FUNCTION__ << endl;
At the start of every function and it displayed this:
main
BigInt
BigInt
BigInt
operator>>
Assign
operator>>
Assign
--This is where the crash occurs.
The obvious first check, when I saw
b.erase(b.begin());
was checking is b was empty,
and of course b was empty which caused you to erase at an invalid location and you got a crash.
Have fun
+ 2
@Dennis
Ill improve the class functions. Thank You!
Im thinking of changing the short array to a string or a vector, as it will allocate memory on their own. Isn't that better to do?
+ 2
If you don't know this one yet,
https://mattmccutchen.net/bigint/
then you should download it and read through the source code.
I like the way that numbers aren't stored per digit, but as whole numbers in an array, which makes things alot more efficient.
+ 2
@Kinshuk Vasisht Yes, you're right, I would recommend the vector.
Or if you feel like practicing you could just make it yourself as well :D
Check out unique_ptrs if you are.
+ 2
@Dennis
So he moves to the next element only when the first element becomes greater than the limit it can allocate?
Thats quite memory efficient. Ill have to work more on this to improve it...
+ 1
@Dennis
Ill revisit those.
But, b isn't empty.
Im using b.erase(b.begin()) to erase the first character from the string, if its a -. How can I do that correctly if this way is wrong? Save a substring from second character to end?
But why does it run fine on a PC?
//I got 'No' Error there...
+ 1
Give me an input that should work for you, I'll try again.
You just got unlucky on pc I guess.
I mean, having a bug and it not causing a crash is probably one of the worst things.
+ 1
@Dennis
Almost any input works on PC. Eg =
10000000000000000000000000000
/
27
or
503784849937
*
446
or
68389478297489399237849
-
78488484
or
874848849
+
874736277
etc.
+ 1
@Dennis
I admit, 1000000 is large, but 100000! has around 425076 digits...
+ 1
@Dennis
And sometimes, the program 'is' running correctly even on Sololearn.
Eg =
Try :
1000
/
2