If you "instantiate" a variable in someFunction (i.e. declare it as non-static; see example below) and return a pointer to it, you end up with undefined behavior. When the function returns, the memory that variable occupied becomes invalid (until used by something else), so the pointer you have to it points to invalid memory. Accessing invalid memory results in undefined behavior. In fact, modern compilers often catch this and warn you about it.
Code:
someType *someFunction(void)
{
someType returnValue;
returnValue.firstComponent = someData1;
returnValue.firstComponent = someData1;
return &returnValue;
}
However, if you declare a pointer inside someFunction, allocate memory to that and return the value, then that's not undefined behavior, but it would cause a memory leak.
Code:
someType *someFunction(void)
{
someType * returnValue;
...
return returnValue; // No & here, returnValue is already a pointer.
}
At POINT 1 (Ptr=0, you overwrite the old value of Ptr (address of memory allocated in someFunction), so you no longer have that address to free the memory later. This is not a problem on the first iteration, Ptr doesn't point to allocated memory yet. Either free the memory at the end of the for loop, or save the address somewhere to be freed later.