2

Is it possible to use dynamic memory allocation, or some other method, to make a 1000-bit (or 1024-bit) integer variable?

Obviously, this is an insane amount, but I'm just using this amount as an example. Basically, can I make an integer any size I want? I'm thinking maybe dynamic memory allocation would work, or perhaps something to do with char arrays?

Mureinik
  • 297,002
  • 52
  • 306
  • 350

2 Answers2

3

Depends on what exactly you mean.
If you mean "a fully functional int that the compiler knows how to handle and can be used with the built-in arithmetic operators", the answer would be no.
If you mean "allocate an arbitrary size of memory that I'll treat as a continuous integer and write my own custom mathematical functions for", the answer is most definitely yes, and there are in fact several libraries that do exactly that.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Perfect! My reasoning for this is a mix of curiosity and practical applications (i.e. writing a code to find giant prime numbers). I'm mostly curious as to how computer are able to store and process numbers with millions of digits. – Victor Resnov Jul 03 '21 at 13:15
2

Yes. For example the GNU Multiple Precision Arithmetic Library (GMP) does this.

orlp
  • 112,504
  • 36
  • 218
  • 315