Please use the code format tags to format your code.
|
unsigned char tab1[min+1];
|
This is not valid C++, it's not flagged as an error because this syntax was introduced in C99, and some compilers import C99/C11 features into C++. In my view, it shouldn't be allowed. I read somewhere in an interview with Dennis Ritchie that a bunch of features slipped into C99 that he wasn't happy with, but admits he didn't read the white papers closely enough, I think this is one of those features.
It means "allocate an array of size
min+1 from the stack".
The problem is, the stack is a limited resource. Modern complex operating systems can grow the stack, but that's not generally true, and on embedded devices, the stack can be as little as 2k.
You've asked for a stack variable of size: 1000001. 1Mbytes is quite a lot.
The correct thing to do is get the memory from the heap, which in C++ can be:
1 2 3 4 5 6 7 8
|
#include <iostream>
#include <memory>
int main() {
std::size_t min = 1000000;
std::unique_ptr<char[]> tab1 = std::make_unique<char[]>(min + 1);
// ...
}
|
A more direct and understandable, but less safe, way is:
1 2 3 4 5 6 7 8
|
#include <iostream>
int main() {
std::size_t min = 1000000;
char* tab1 = new char[min + 1];
// ...
delete [] tab1;
}
|
I've used
char
instead of
unsigned char
because it seems you mean
char
from
tab[input] = '1';
, as
'1'
is a
char
.