473,471 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Heap/Stack Allocations - Are my assumptions correct?

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.

using System;

namespace MyTest
{
class Class1
{
static int a = 0; // Allocated on the heap.

static void Main(string[] args)
{
int b = 0; // Allocated on the stack.
b = a;
}
}

class Class2
{
int c = 5; // Allocated where?

public int Test(int X)
{
int d = 1; // Allocated on the stack.

return d * X * c;
}
}
}

-Nick McCamy

Nov 15 '05 #1
2 2839
a is allocated when your class is first loaded into memory, and it will
always remain allocated (unless the assembly gets unloaded)
I don't know exactly where it gets allocated but it is probably more in some
area called "static data" than on the heap itself (it won't be GCed). It is
not on the stack, unless you qualify it with the [ThreadStatic] attribute,
in which case it will be at the very bottom of the stack.

b is allocated on the stack, inside the current stack frame.

c is allocated inline inside the instances of Class2, which are themselves
allocated on the heap. So, c is indirectly allocated on the heap.

d is allocated on the stack, nside the current stack frame.

So, the statement saying that instances of value types are allocated on the
stack is wrong. In the case of c, the value is allocated indirectly on the
heap. And, in the case of a, I don't know exactly where it gets allocated
(could be on the heap, could be in some "static data" area), but it does not
matter because it will always be there.

Bruno.

"Nick McCamy" <nm*****@carolina.rr.com> a écrit dans le message de
news:20042216827.969865@lexundesigns...
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 found
within a method.

using System;

namespace MyTest
{
class Class1
{
static int a = 0; // Allocated on the heap.

static void Main(string[] args)
{
int b = 0; // Allocated on the stack.
b = a;
}
}

class Class2
{
int c = 5; // Allocated where?

public int Test(int X)
{
int d = 1; // Allocated on the stack.

return d * X * c;
}
}
}

-Nick McCamy
Nov 15 '05 #2
Nick McCamy <nm*****@carolina.rr.com> wrote:
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
Yes.
- 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


No - b and d are both allocated on the stack because they're local
variables.

Variables themselves are:

- On the stack if...
They are instance variables of a struct, and the struct itself is
on the stack.
They are local variables.

- On the heap if...
They are instance variables of a reference type.
They are instance variables of a struct, and the struct itself is
on the heap.
They are static variables.

Note that the value of a reference type variable is a reference - so
while the object itself will always live on the heap, the reference can
live either in the stack or in the heap, depending on the above rules.

See http://www.pobox.com/~skeet/csharp/memory.html for more
information.

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

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

Similar topics

22
by: MSG | last post by:
Hello void f1(int n) { vector<int> x(n); /* C++ */ } void f2(int n) { int x; /* C99 only */ } void f3(int n) { int* x = new int; /* C++ */ delete x; }
17
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; };
4
by: ivan | last post by:
Hi, How can we calculate the stack and heap size requried by a C program. Is there any specific formula used? Please suggest. regards, Ivan
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
11
by: Andy Watson | last post by:
I have an application that scans and processes a bunch of text files. The content I'm pulling out and holding in memory is at least 200MB. I'd love to be able to tell the CPython virtual machine...
9
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default...
11
by: Nehil | last post by:
I would like to know which is dynamic in nature. if i refer the C memory model (Richard Steven), it is shown that both stack and heap grow towards each other. Now, can one go into other's area and...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.