4

Well, the question is not as silly as it sound.

I am using C++11 <array> and want to declare an array like this:

array<int, MAX_ARR_SIZE> myArr;

The MAX_ARR_SIZE is to be defined in a header file and could be very large i.e. 10^13. Currently I am typing it like a pre-school kid

 #define MAX_ARR_SIZE 1000000000000000

I can live with it if there is no alternative. I can't use pow(10, 13) here since it can not be evaluated at compile time; array initialization will fail. I am not aware of any shorthand to type this.

9
  • 3
    Well you could do const size_t max_array_size = 10e15;, but an array that size is likely too large for the stack. Having a MAX_ARR_SIZE indicates that you have some kind of dynamic sizing; is there a reason you aren't using std::vector? Commented Mar 31, 2016 at 10:18
  • 1
    @TartanLlama or constexpr size_t max_array_size = 10e15; Commented Mar 31, 2016 at 10:19
  • 4
    This smells like an XY problem Commented Mar 31, 2016 at 10:26
  • 1
    @Dilawar no, it's type size_t (unsigned interger type) Commented Mar 31, 2016 at 10:36
  • 3
    note: this will fail if your system doesn't have 4000 (or 8000) terabytes of ram free .... Commented Mar 31, 2016 at 10:49

6 Answers 6

9

Using #define for constants is more a way of C than C++.

You can define your constant in this way:

const size_t MAX_ARR_SIZE(1e15); 
Sign up to request clarification or add additional context in comments.

1 Comment

7

In this case, using a const size_t instead of #define is preferred.


I'd like to add that, since C++14, when writing integer literals, you could add the optional single quotes as separator.

1'000'000'000'000'000

This looks more clear.

Comments

2

You can define a constexpr function:

constexpr size_t MAX_ARR_SIZE()
{
    return pow(10, 15); 
}

That way you can do even more complex calculations in compile time.

Then use it as array<int, MAX_ARR_SIZE()> myArr; it will be evaluated in compile time.

Also like it was already mentioned, you probably won't be able to allocate that size on the stack.

EDIT:

I have a fault here, since pow itself is not constexpr you can't use it, but it's solvable, for example use ipow as discussed here: c++11 fast constexpr integer powers

here is the function quote:

constexpr int64_t ipow(int64_t base, int exp, int64_t result = 1) {
  return exp < 1 ? result : ipow(base*base, exp/2, (exp % 2) ? result*base : result);
}

simply change MAX_ARR_SIZE() to:

constexpr size_t MAX_ARR_SIZE()
{
    return ipow(10, 15); 
}

2 Comments

This is great. It solves this problem and a great general tip.
@dilawar if it solved your problem, don't forget to accept the answer ;)
1
#define MAX_ARRAY_SIZE (1000ull * 1000 * 1000 * 1000 * 1000)

Comments

0

You actually can evaluate pow(10, 15) and similar expressions at compile time in C++11 if you use a const instead of #define. Just make sure you pick a large enough primitive.

Comments

0

You can use :

#define MAX_ARR_SIZE 1e15

1e15 is very huge and probably would not be allocated.

2 Comments

Ten times too big.
@gnasher yes i fixed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.