The above figure
represents the IBM compactable pc architecture. We can clearly identify from
the figure that there are mainly two areas stack and heap. The stack area is
again divided in to two parts as shown in figure. They are member variables area or private area, and
member function area or public area . The heap contains the global variable and
static variables.
In stack area public interaction
is very less. That is we cannot access
the stack area directly without the help of objects. The heap area is the area
where high public interactions occurs because of this we write all the programs
with static main and that lies in heap area.
All the main( )
function lies in the heap area because we defined it as static. In the figure
we can see a barrier between heap area and stack area, that means we can’t
access the stack area directly. Os initiate the program in heap area as shown
in figure. But we can’t go to stack area. For accessing stack area member
functions we need the help of objects. Here come the duty of objects; the real
definition or function of object is that to make accessible the stack area. Through
objects we can access only member functions and with the help of member
functions we can access the member variables too. The static main( ) functions are lies in heap
area but if we define a function without static key word it lies in stack area
and we or os cannot access it directly. This makes our program error. We want
to go through some codes for understand the above more clearly. All the codes I
use in these tutorials are C# codes.
[Code]
Using system;
Class sample {
Public static void main ( ) {
int x,y,s=0;
X=5;
Y=20;
S= sum(x,y);
System .console.writeline(“sum=”+s);
}
public
int sum(int x, int y){
return(x+y);
}
}
[/code]
Is there any problem in the above code?. If u understands
the above explained architecture or you have much experience in programming you
can find the error of previous program within seconds. There is a compilation error
in the above program. Before going to the error I would like to tell something.
This is a console application program. You can see the console.write line
statement there and the ‘+’ operator is used as a concatenate operator in C#.
Come to the error back, this mistake is actually architectural mistake. I’ll
give two alternatives to this program from those you can understand what error
occurred and what is the reason for that ok?.
1
[Code]
Using system;
Class sample {
Public static void main ( ) {
int x,y,s=0;
X=5;
Y=20;
sample
sa=new sample( );
s=sa.sum(x,y);
System .Console.Writeline(“sum=”+s);
}
public
int sum(int x, int y){
return(x+y);
}
}
[/code]
2
[Code]
Using system;
Class sample {
Public static void main ( ) {
int x,y,s=0;
X=5;
Y=20;
s=sum(x,y);
System .Console.Writeline(“sum=”+s);
}
Public
static int sum(int x, int y){
return(x+y);
}
}
[/code]
I hope you got the
reason from the above codes. Don’t worry who didn’t get the reason even reading
the above codes. I’ll explain for you people.
Look at our
architecture figure once more that picture. Put the picture in your mind we
need that ok?. In first code(the code with problem), what is happening there what is the problem
there. In the line 7 s=sum(x,y); how this statement work?. The function sum
can’t accessed there because see the picture member functions are in stack
area. Can u access that?. With the help of an object you can access that that
is shown in code 1
and
please don’t follow the second one it is not the actual method. The second one
I give to u to explain the architecture. What happened in second one? The
function is declared as static so it lies in heap area so we can access it
anywhere from the program because it has the global scope. I hope it is clear
for all. We here define object as, the
object is the authorized entity or member to access the stack area from heap
area. I think u all got a clear idea about objet and the fog on the object
is wiped from your mind.
All most all programming
languages support public static void main( ) because of security reason. As I
said before the second method is not perfect and it is not preferable. It is
not a good method to practice it is less secure and not a good programming
concept. For programs the start up will be in heap area then access the stack.
I’ll give a code to u please do it before do it in system go through it and
analyze it. After analyzing come to a conclusion whether it works or not?
Work
[Code]
Using system;
Class sample {
Public static void main ( ) {
int x,y,s=0;
X=5;
Y=20;
sample sa=new sample( );
s=sa.sum(x,y);
System .Console.Writeline(“sum=”+s);
}
public
int sum(int x, int y){
show( );
return(x+y);
}
public
void show( ){
Console.Writeline(“Hai”);
}
}
[/code]
No comments:
Post a Comment