Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Unlike C (even C99/C11), C++ allows initializers in if-conditions, so this compiles.
// Unlike C (even C99/C11), C++ allows initializers in if-conditions, so this compiles. #include <assert.h> #include <stdio.h> #include <stdlib.h> static unsigned long long *fibonacci(const size_t length) { assert(length > 1); if (unsigned long long *const fib = static_cast<unsigned long long *>(calloc(length, sizeof *fib))) { fib[0] = 0; fib[1] = 1; for (size_t i = 2; i != length; ++i) fib[i] = fib[i - 2] + fib[i - 1]; return fib; } return NULL; } int main(int argc, char **argv) { (void)argc; static const size_t length = 94; if (unsigned long long *const fib = fibonacci(length)) { printf("[ "); for (size_t i = 0; i != length; ++i) printf("%llu ", fib[i]); printf("]\n"); free(fib); exit(EXIT_SUCCESS); } fprintf(stderr, "%s: error: out of memory\n", argv[0]); return EXIT_FAILURE; }
run
|
edit
|
history
|
help
0
Check if a year is leap year or not
Linker error while taking the address of a constexpr variable
Wrapper to pass shared_ptr to &, const &, *, const * through std::bind.
CS1428 SI Tuesday
ternary test c++14
GraphBase
boost::geometry::distance performance overhead compared to a straightforward implementation
Unpacking tuple
Forgetting to check end
LOOL