int* makeiniele()
{
int res[55] = { 0 };
return res;
}
The problem here lies with scope - once a function ends, any variables declared within the function are deleted from memory. Once the function makeiniele
terminates, the array res
is also deleted from memory. The memory that array took up has been replaced with something else - this 'something else' is what's being printed to your screen. You can verify this with the following code:
int * myvar = new int[55]{ 0 };
int* var = myvar;
printele(var);
delete myvar;
printele(var);
The fist 55 numbers will be zeroes (the variable hasn't been deleted yet). Once the variable is manually deleted with delete
the address of it is now pointing to unused memory, so it spits out random numbers. To fix this, replace your makeiniele
funciton with this:
int* makeiniele()
{
int * myvar = new int[55]{ 0 };
return myvar;
}
And everything should work fine!
EDIT: As Zereges said, you MUST ensure to end your program with
delete myvar;
to prevent a memory leak.