This question already has answers here:
Does C++ support Variable Length Arrays?
(4 answers)
Why does a C/C++ compiler need know the size of an array at compile time?
(6 answers)
Closed 5 years ago.
void merge(vector<Flight>& data, int low, int high, int mid, string criteria)
{
int i, j, k, temp[high - low + 1];
...
The error that comes up is "the value of parameter "high" (declared at line 100) cannot be used as a constant". I haven't managed to find an appropriate answer to this question online.
high - low + 1 needs to be a compile time evaluable constant expression in C++. (C++ does not support variable length arrays.)
And that isn't, so the compiler issues a diagnostic.
The simple solution is to use a std::vector<int> as the type for temp.
Related
This question already has answers here:
When does a constexpr function get evaluated at compile time?
(2 answers)
constexpr function not calculate value in compile time
(4 answers)
Closed 3 years ago.
constexpr unsigned long long factorial(unsigned long long n)
{
return n > 1 ? n * factorial(n - 1) : 1;
}
I have just learnt that if I call a constexpr function using a value known at compile time, the compiler will just replace that function with his final result.
So why do I get a crash at runtime after compiling this call to my function?
std::cout << factorial(10000);
If it was evaluated at compile time, I would get an error just builing my project and it wouldn't even start...
What's wrong?
This question already has answers here:
Why does the most negative int value cause an error about ambiguous function overloads?
(3 answers)
Closed 3 years ago.
I'm trying to write a test case for some corner case. For input of type int64_t, the following line won't compile:
int64_t a = -9223372036854775808LL;
The error/warning is:
error: integer constant is so large that it is unsigned [-Werror]
I thought the number was out of range, so I tried:
std::cout << std::numeric_limits<int64_t>::min() << std::endl;
It outputs exactly the same number!!! So the constant is within the range.
How can I fix this error?
You may write
int64_t a = -1 - 9223372036854775807LL;
The problem is that the - is not part of the literal, it is unary minus. So the compiler first sees 9223372036854775808LL (out of range for signed int64_t) and then finds the negative of this.
By applying binary minus, we can use two literals which are each in range.
Ben's already explained the reason, here's two other possible solutions.
Try this
int64_t a = INT64_MIN;
or this
int64_t a = std::numeric_limits<int64_t>::min();
This question already has answers here:
unsigned int vs. size_t
(8 answers)
Closed 6 years ago.
I saw an example recently that looks like the following:
const size_t NDim = 3;
double coords[NDim];
My question is straight forward. When does one use size_t vs an int or unsigned int? In this particular case, wouldn't the following be the equivalent as the above:
const unsigned int NDim = 3;
double coords[NDim];
size_t is commonly used for array indexing and loop counting.
According to cppreference:
Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.
It also states:
std::size_t can store the maximum size of a theoretically possible
object of any type (including array). A type whose size cannot be
represented by std::size_t is ill-formed (since C++14)
The answer is straightforward as well. You use size_t for all your array indexing and sizing needs, this is exactly what it was designed for. And you never use anything else for it.
Apart from being a self-documenting feature, it also has another important aspect - on many platforms sizeof(int) is not equal to sizeof(size_t).
This question already has answers here:
Handle arbitrary length integers in C++
(3 answers)
Closed 7 years ago.
Hi I am doing an algorithm question require get the full result of
5208334^2, which is 27126743055556
I was able to do it with by represent integer using Charracter array. However can we have any better way (shorter or faster) to do that? any idea is welcome ?
Updated:
For my case, both long long and int64 work, just that I did not cast value before return:
int val (int n1, n2) {
........
return (long long) n1 * n2;
}
This number fits into long long(present in GCC and after c++11) type or int64(for some other compilers before c++11). Thus the simplest solution is to use this type.
This question already has answers here:
What does this C statement mean?
(4 answers)
Closed 10 years ago.
What does those notations refer to ? What am i declaring ? I have a hard problem identifying what is being declared
1 - double (*b)[n];
2 - double (*c[n])();
3 - double (*d())[n];
And i can't even think in understanding this one
double (*foo(double (*) (double, double[]),double)) (double, ...);
Overall, if there is any logic or step-by-step i can use for almost all ( or preferably al )
cases, would be really great to know.
double (*b)[n];
This is a pointer named b that points to an array of doubles that has length n.
double (*c[n])();
This is an array named c of n pointers to functions that take in unspecified arguments (in C) or no arguments (in C++) and return doubles.
double (*d())[n];
This is a function named d that returns a pointer to an array of n doubles.
Hope this helps!