Please Have a Look at my Binary converter and give me some advice on how to optimise my code. Pointers appreciated. Thanks | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please Have a Look at my Binary converter and give me some advice on how to optimise my code. Pointers appreciated. Thanks

https://code.sololearn.com/crUlugUkpKBl/?ref=app

21st Apr 2020, 8:11 PM
Saad Ousman
Saad Ousman - avatar
6 Answers
+ 1
You actually can't do that with an array. They are immutable. You would need to create a new array each time you wanted to change its length.
22nd Apr 2020, 8:43 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
It's dangerous the way you're doing this. You're writing into memory beyond the end of the array and then recalling those bits from memory. While it is working here you may end up overwriting something important when ran on a computer or you may end up receiving some garbage back from accessing the elements beyond the zero index. Instead create the array with an element count that is large enough to hold all integers within the bounds of an integer type. sizeof(type) * 8
21st Apr 2020, 10:01 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Got it. Thanks alot!
22nd Apr 2020, 8:23 AM
Saad Ousman
Saad Ousman - avatar
+ 1
Thanks dawg. Much appreciated
22nd Apr 2020, 4:45 PM
Saad Ousman
Saad Ousman - avatar
+ 1
You don't really need them here, but when you do get to where you need something similar to an array, yet resizable, look into the STL at Containers and see vectors and lists etc.
22nd Apr 2020, 4:52 PM
ChaoticDawg
ChaoticDawg - avatar
0
I tried to write it in a way that would increment the the array size by one everytime an element is added. Bad method huh
22nd Apr 2020, 8:25 AM
Saad Ousman
Saad Ousman - avatar