Once it was mentioned that the functions dont occupy any memory when we write them in the class.Again at a later stage it was mentioned that when we create multiple objects thn members functions occupy a common memory and not created individually for diff objects. Pls clarify it to me
Memory occupied by the member functions of the class
@Senjuti256,
Functions don’t occupy memory when they are declared, they are allocated stack memory only when the function is called. So when we write a function in class, it does not occupy any memory, but latter when it is called, it is allocated memory.
everytime we create an object fn is called so how will memory be allocated ?Will it be allocated memory everytime with each object creation?
say when we are calling same function but with different objects thn will the same function occupy 2 different portions of stack memory?
@Senjuti256,
When you are calling a function, it is allocated some memory on stack, only till the time it executes. As the return statement is encountered, the memory is freed control goes back to the calling function. Thus is you call it twice using different functions, the memory will be allocated and freed for the first call, and then the same would happen for the second call.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.