Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.47/5 (8 votes)
See more:
Hi All,

I know the functions are loaded only once in the memory. My doubt is... if we create an object of a class how it refer the function? What is the thing behind the object creation and function calling? Is there any pointer in the object to the function?
Posted
Updated 20-Nov-11 22:16pm
v3

If you declare a method of a class or a structure, only one piece of code implementing this method is created, one per all instances. Why? Well… just because OOP wasn't created by idiots. :-)

Now, the question is: the same method called on different instances of the same calls apparently does different job, well, because it works with different instance. The question is simple: it works exactly as a static methods, with one simple difference: it has one extra parameter which is implicitly passed to a method; and this parameter is a reference to that very instance. And guess what is this? This is a parameter "this".

For some more explanations, please see my past solution: What makes static methods accessible?[^].

One more interesting fact: .NET code is JIT-compiled. When the code is compiled to the native CPU instructions? In fact, it is compiled on demand on per-method basic. If some method are linked into the assembly but never called, this method will never be JIT-compiled during the whole run time.

[EDIT]

Read this article for in-depth information on creating the run-time objects behind the scene: http://msdn.microsoft.com/en-us/magazine/cc163791.aspx[^].

—SA
[I have no idea why so much white space is rendered below — SA]
 
Share this answer
 
v6
Comments
Silju MC 22-Nov-11 6:12am    
You are right... :-) my doubt is.....

Class MyClass
{
int x;

void Function()
{
}
}

MyClass obj = null;
obj.Function();



In this code snippet how the exception is coming (obj.Function();)? What is happening behind? I think the function is already in the memory. How it doesn't get the reference? What is happening when we create an object? I know when we create an object only the members allocated with sufficient memory in heap.
Sergey Alexandrovich Kryukov 23-Nov-11 1:17am    
First, there is nothing about exceptions in your code.

Now, I explained how the reference is passed to an instant (non-static) method "Function". It has one implicit parameter "this" which is passed the the code. Please see my other solutions referenced above.

It can be expressed by some pseudo-code like this:

obj.Function(); //real code

works like

MyClass.Function(obj); //equivalent pseudo-code, what happens behind the scene.

Is it clear?

What happens when an object is created? Yes, it is created on heap, and initialized, first, per member (x = 0 as default value) and then a constructor is called. In your case this is a default constructor MyClass() which actually does nothing.

As to the code itself, you can think that it is already in memory, at least logically, from the moment of loading the application. Physically this is not so. .NET code is compiled into native code first, using JIT-compilation (Just-in-Time), but it happens on demand on per-method basis. In other words, if MyClass.Function is never called, if will never be created. It is created right before a method is called for the very first time. For other calls (same instance of any new instances, does not matter), already-created code is used.

Is that clear, too?

--SA
Silju MC 23-Nov-11 23:43pm    
Thank you SA. :-) 5+ and I'm accepting your answer.
Silju MC 23-Nov-11 23:14pm    
Thank you SA. :-) 5+ and I'm accepting your answer.
Sergey Alexandrovich Kryukov 23-Nov-11 23:28pm    
Great. Hope it's helpful.
You're very welcome. I really appreciate you're trying to dig out the very essence of things. This kind of interest is very fruitful and will pay off very well in near future.

Good luck, call again.
--SA
Further to SA's answer, the reason only one version of each class is needed is down to the memory map.
The variables are per-instance, it is the functional steps that are per-class. If a method references this.foo, then the code will look at the memory in the heap for the current object this], and retrieve the item for foo. For another instance, the code is the same, just the memory being used is different.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Nov-11 1:19am    
It is really more complex than that due to JIT compilation. Please see my long comment below my solution, in response to OP's follow-up questions.
--SA
Sergey Alexandrovich Kryukov 23-Nov-11 1:28am    
See also the new link, the last on in my solution, after [EDIT] -- very interesting.
--SA
CSS
Hi,

Here is resume on programs memory structure :

-Stack : It is used for holding the return address at function calls, arguments passed and local variable for functions; it also stores the current state of the CPU.
-Heap : Memory area is a region of free memory from where memory is allocated for the dynamic allocation in C++.
-Global Variable : This space is used to store the global variable used in the program.
-Program Code : This region holds the compiled code of the program.

For member functions, the code for those is not different from free-function code in terms of where it goes in the executable image : "Program Code" Area.

After all, a member function is basically a free function with an implicit "this" pointer as its first argument.


When an object is allocating with "new", a new instance of members vars are allocated in the Heap but a member function code is associated to the class and stored just one time in "Program Code" Area.



this was my solution to the posted question Where th function is stored[^]
 
Share this answer
 
Comments
Silju MC 23-Nov-11 23:31pm    
5+ :-) Accepted
elgaabeb 24-Nov-11 3:37am    
u r welcome :)
you can contact me by my website: http://thevivek.pro

the object is the functionality of java and it's calling by the creating of it.

the java language is object oriented language so.

Java knows the object.

The object is one kind of reference.

When object called it refers the address of the particular method or class..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900