Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Java
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 20-Nov-11 23:08pm
Edited 20-Nov-11 23:16pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Smile | :)
 
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]
  Permalink  
v6
Comments
Silju MC at 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.
SAKryukov at 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 at 23-Nov-11 23:43pm
   
Thank you SA. :-) 5+ and I'm accepting your answer.
Silju MC at 23-Nov-11 23:14pm
   
Thank you SA. :-) 5+ and I'm accepting your answer.
SAKryukov at 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
Silju MC at 23-Nov-11 23:44pm
   
Thank you so much :-)
Silju MC at 23-Nov-11 23:45pm
   
Your answer always helpful to me.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
SAKryukov at 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
SAKryukov at 23-Nov-11 1:28am
   
See also the new link, the last on in my solution, after [EDIT] -- very interesting.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  
Comments
Silju MC at 23-Nov-11 23:31pm
   
5+ :-) Accepted
elgaabeb at 24-Nov-11 3:37am
   
u r welcome :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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..
  Permalink  

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

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 273
2 Aajmot Sk 224
3 Marcin Kozub 210
4 Richard MacCutchan 200
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100