How to increase memory for a single variable?

So, I've made programs like Prime number searchers and such. But the problem is if I use an int or long int variable for the program I am limited by the variable size. I can't search through numbers larger than their memory size. So my question is: Is there a way to allocate memory to a single variable, NOT AN ARRAY, so I can make a variable as many bytes as I want?

Thanks.
> So my question is: Is there a way to allocate memory to a single variable,
> NOT AN ARRAY, so I can make a variable as many bytes as I want?

The range of fundamental numeric types are limited.
http://en.cppreference.com/w/cpp/types/numeric_limits

It is possible to create a user-defined numeric type that has a larger range. (Internally, the implementation would use something like an array, but we need to concern ourselves with those details.)

For instance, using boost::multiprecision::cpp_int

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>

int main()
{
    using bigint = boost::multiprecision::cpp_int ;

    bigint i( "1234567890987654321" ) ;

    std::cout << i * i * i * i << '\n' ;
}

http://coliru.stacked-crooked.com/a/00566cc2f9881902
You're basically asking if C++ has arbitrary-precision integers (bignums) in its standard library. The answer is unfortunately no (as far as I know).

Boost has a library for them, though:
http://www.boost.org/doc/libs/1_53_0/libs/multiprecision/doc/html/boost_multiprecision/intro.html

Otherwise, you could go about creating a class or typedef for arbitrary-precision arithmetic (though there are plenty of good libraries for this already), in which you'll probably end up using an array/vector and having a LOT of overloaded operators. >_>

There was a thread about creating bignums on the forum some time ago. If you decide to implement your own, you may want to take a look at it:
http://www.cplusplus.com/forum/lounge/32041/

-Albatross
Thank you. I'll give those a try.
Topic archived. No new replies allowed.