Is it correct to return a memory address in function?

Asked

Viewed 598 times

7

I was asked to design a function that was called, open a file. txt and return the pointer of this file, ie return a memory address. However as I know memory is only temporarily allocated for functions, i.e., after the execution of the function the memory is free to be used by another part of the program.

I think the most correct would be to declare the pointer to file in a main function (main) and then send the pointer as a parameter to a function that opened the file and "pointed" to the memory address that was sent. I am correct?

1 answer

7


There is a misconception in your thinking, perhaps you lack better understanding about stack and heap.

If the information is on stack really you can’t trust that it will be there when the function ends, but it is a little rarer you create a pointer to the stack.

But if it’s allocated to heap, you can return a pointer to this allocated area smoothly. The area remains available even when the function is finished. This is the main reason to use the heap.

You are even correct in your thinking from the point of view of code organization. The most correct is the function that needs information to allocate the memory needed for the object, pass it to the function that will fill this object and then when it comes back to it to release this memory.

Nothing prevents you allocating in a function that will fill and return, but it is asymmetrical, because it is responsible for allocating and does not release (nor could it in this case), this gives margin for errors. Well-built functions require consumers to deliver their already-allocated memory. Even if you need a relocation within it, this is less problematic. Interestingly, this is basically the so-called dependency injection.

So from a technical point of view you can do as you’re asked, but any good programmer would do it the way you’re thinking, you’re right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.