size_t is an integer type, and no cast is required.
In C ++, if you want to have an array with dynamic size, you need to use dynamic allocation with new . That is, you cannot use:
std::size_t sz = 42; char carray[sz];
Instead, you need to use the following:
std::size_t sz = 42; char* carray = new char[sz];
or, preferably, you can use std::vector ( std::vector manages memory for you, so you don’t need to remember it explicitly, and you don’t need to worry about many of the ownership problems that come with dynamic allocation manually):
std::size_t sz = 42; std::vector<char> cvector(sz);
source share