Is the following code ok?:
std::vector<char> var;
size_t requiredSize;
getenv_s(&requiredSize, NULL, 0, "Something");
if (requiredSize == 0)
{
return ENV_NOT_EXIST;
}
if(var.size() < requiredSize)
var.resize(requiredSize);
// Get the value of the environment variable.
getenv_s(&requiredSize, &var[0], requiredSize, "Something");
std::string str(var.begin(),var.end());
If this code is OK, can someone please explain me how the begin()
and the end()
values of the var
vector are updated? it looks like this code changes directly the internal array of the vector, not over the std::vector
api - so how these values are updated to the actual size?
std::vector
magically changes its name fromvar
tosvar
tovar
again, yes that's save to do. – Ay