I have declared and initialized a (dynamic?) array inside a bool
function, as seen in this simple example below. I'm using the same function to populate it. I don't want the array to remain preserved in the computer's memory once the function's execution is complete to allow the array to be repopulated with the correct values in future function calls. Will it be automatically erased from the memory once the function call is returned? If not, what's the workaround for it?
bool correct_count(int start, int end)
{
int numbers[] = {};
for (int i = 0; i < (end - start + 1); i++)
{
numbers[i] = start + i;
}
if (numbers[0] == start && numbers[end - start] == end)
{
return true;
}
return false;
}
In this simple example, we can determine the size of the array, but that is not the case with the program I'm trying to write. Also, I'm not allowed to use memory-related operators like the dereference operator or dynamic memory allocation just yet for my programs, because that's currently out of the scope of what's already been taught to me.