Stack vs heap

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

DataSet ds = new DataSet()

Is the variable (pointer) 'ds' in the stack and the dataset object being pointed allocated in the heap

Thanks
Benny
 
yes, DataSet object resides in the heap and 'pointer' is on the stack
if you wont preserve that pointer (as method return value) the pointer will
be lost thus leading to DataSet object collection by GC
 
Benny said:
DataSet ds = new DataSet();

Is the variable (pointer) 'ds' in the stack and the dataset object being
pointed allocated in the heap?

The DataSet itself is definitely on the heap. If ds is a local variable
within a method, it's on the stack. If it's a class or instance
variable, it's on the heap too.

See http://www.pobox.com/~skeet/csharp/memory.html for more
information.
 
Vadym Stetsyak said:
yes, DataSet object resides in the heap and 'pointer' is on the stack
if you wont preserve that pointer (as method return value) the pointer will
be lost thus leading to DataSet object collection by GC

Just a note that reference can be preserved in other ways, for example
storing it as class scope field.
 
If that is the case, why is it being said that static methods can't access instance variables

Static methods will be in the stack right (or is it?). And the instance pointers will be in the stack too. So isn't it possible for static methods to 'indirectly' access instance variables through the instace pointers in the stack?
 
Rakesh said:
If that is the case, why is it being said that static methods can't
access instance variables?

Static methods will be in the stack right (or is it?).

What exactly do you mean by this?
And the instance pointers will be in the stack too.

Or this?
So isn't it possible for static methods to 'indirectly' access
instance variables through the instace pointers in the stack?

I think you really need to explain carefully exactly what you mean.
Here's an example program which you should think about:

using System;

class Test
{
int x;

Test (int x)
{
this.x = x;
}

static void Main()
{
Test t1 = new Test(5);
Test t2 = new Test(10);
PrintX();
}

static void PrintX()
{
Console.WriteLine(x);
}
}

It won't compile, because the static method doesn't have access to the
instance variable x.

However, suppose you *could* compile it, as it looks like you want to.
What would it print? 5 or 10? What would happen if you hadn't created
any instances at all?
 
Static methods will be in the stack right (or is it?).

What does it mean "Static methods will be in the stack"?
Static methods can be accessed only through the type (e.g. class) if you
will pass instance variable as parameter to static method - that's okay, you
will be able to use them (only with public modifier)
 
Hi,
If that is the case, why is it being said that static methods can't access
instance variables?

You are confusing two differents things, it has nothing to do with where
the variables are kept, static methods cannot access instance variables for
different reasons, a call to a static method is done from a class
perspective, Class.StaticMethod() you don't need to have an instance to
call it, therefore you cannot use a variable that is dependand of an
instance to exist.

Static methods will be in the stack right (or is it?). And the instance
pointers will be in the stack too.


No, in the stack you will never find code, the code is kept somewhere else,
the stack is only used to temporary placement of local variables and to keep
a pointer to where return the execution when the method finish the
execution.
So isn't it possible for static methods to 'indirectly' access instance
variables through the instace pointers in the stack?

Why are you assuming that an instance exist?
Maybe an example is good here, take the Math class of the framework, you
NEVER create an instance of it, but you use its static methods.


If you need to have access to instance variables you can do so by passing
it as a parameter:

class A{
private int i;
static int m( A a)
{
a.i = a.i *2;
return a.i;
}
}


As you see you have access to the private members of the instance. Is this
what you are trying to do?


Hope this help,
 
I am extremely sorry to have been vague in describing my issue. I am not thinking in the OOPS perpective, I am thinking in the internal compiler perpective

Dataset ds = new Dataset()
static DataSet sds = new Dataset()
static int x = 0

From what I understand, the pointer ds is stored in the stack, and the object new Dataset() is allocated in the heap. Also, pointer sds is in the stack, but where is this object allocatd? - heap or data area? Where is the associated class being allocated

I am just curious on how exaclty things are done by the compiler

Thanks a lot

Rakes
 
Rakesh said:
I am extremely sorry to have been vague in describing my issue. I am
not thinking in the OOPS perpective, I am thinking in the internal
compiler perpective.

Dataset ds = new Dataset();
static DataSet sds = new Dataset();
static int x = 0;

From what I understand, the pointer ds is stored in the stack, and
the object new Dataset() is allocated in the heap.

That depends on where that code is. You've given a snippet without any
context. Given that you've got the "static" modifier, I'll assume that
ds is an instance variable - in which case, no, it's not on the stack,
it's on the heap (as are the other two).

As I said before, see http://www.pobox.com/~skeet/csharp/memory.html
for a more detailed explanation.
 
Rakesh said:
Where are the method/class defenitions stored - global mem area?

Basically, yes. Actually, I believe they're stored on a per-AppDomain
basis, but for most intents and purposes that's global.
 
Rakesh said:
Wow!!! Thanks a lot!

Do you know of any other links that explains internal concepts like these?

Not really - it's just always something that's made sense to me...
 
Hi Rakesh,
If that is the case, why is it being said that static methods can't access
instance variables?

I haven't heard this before. You can access any isntace members from a
static method as long as you have a reference to the instace of interest.

The only diference (besides the syntax of using static and isntace methods,
which is language specific) is that instance methods receive one parameter
more, which is reference to the instance of the class (data part of the
class). Static methods doesn't receive this parameter so they are not bound
to any instance. If you pass explicitly a reference to a class instace to a
static class method you will have access to all instace members.
Static methods will be in the stack right (or is it?).

I assume this is a typo. The stack and the heap are memory structures where
only data is kept.
And the instance pointers will be in the stack too. So isn't it possible
for static methods to 'indirectly' access instance variables through the
instace pointers in the stack?

Yes, they can. Static methods are like any other method (with the difference
I explained above)
Aassuming that they have reference to the istance they can access all
members that has proper access modifier . Static methods can access any
other member declared in the same class as the static method, regardless of
the access modifier. If the static method wants to access members of other
classes they have to have proper access modifiers.
 
Back
Top