Victor Resnov
Victor Resnov

Reputation: 292

Is it possible to create an integer of any size with dynamic memory?

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?

Upvotes: 2

Views: 396

Answers (2)

Mureinik
Mureinik

Reputation: 311228

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.

Upvotes: 3

orlp
orlp

Reputation: 117681

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

Upvotes: 2

Related Questions