473,840 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stack vs heap

DataSet ds = new DataSet()

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

Thanks
Benny
Nov 15 '05 #1
12 2537
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" <an*******@disc ussions.microso ft.com> wrote in message
news:90******** *************** ***********@mic rosoft.com...
DataSet ds = new DataSet();

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

Nov 15 '05 #2
Benny <an*******@disc ussions.microso ft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

"Vadym Stetsyak" <pd****@ukr.net > wrote in message
news:e2******** ******@TK2MSFTN GP11.phx.gbl...
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.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
Nov 15 '05 #4
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?
Nov 15 '05 #5
Rakesh <an*******@disc ussions.microso ft.com> wrote:
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.WriteLi ne(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?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
> 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)
Nov 15 '05 #7
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.StaticMet hod() 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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 15 '05 #8
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
Nov 15 '05 #9
Rakesh <an*******@disc ussions.microso ft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
30107
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
17
5056
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
1
3771
by: Geiregat Jonas | last post by:
I'm reading Eric Gunnerson's book. He is talking about the heap and stack, he says you have 2types, value wich are in the stack or inline or reference types wich are in the heap. I don't get this what's heap stack and what's the main difference between those 2types ?
2
2861
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is allocated on the stack since it's a value type variable - variable d is allocated on the stack since it's a value type variable Where does variable "c" get allocated? It's a value type, but not foundwithin a method.
3
1713
by: nahur | last post by:
why do you need a heap and a stack why not all memory called a heap or call it a stack what is the purpose of having a heap and a stack
13
2318
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string property(variable). The string would be a reference type being contained in a value type. Would this filter up and cause the stack to now be a reference type placed on the heap or would it still be on the stack with a pointer used internally to point to a...
9
3390
by: shine | last post by:
what is the difference between a heap and a stack?
24
2902
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as are structs, where classes are on the heap... when value types go out of scope the memory is re- allocated, object remain in memory waiting to be cleaned up by the garbage collector, etc, but he responded 'so why not just put say a class on the...
16
4455
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
9
3177
by: Roman Mashak | last post by:
Hello, I'm confused about heap and stack memories management in C language. Most books explain that local stack variables for each function are automatically allocated when function starts and deallocated when it exits. In contrast, malloc() always takes memory in the heap. Now, let's consider the code as follows: int get_buffer() {
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10657
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9436
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7836
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.