473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# structs and the heap question

Hi,

I have a question about how C# handles struct. From the help, struct
variables are stored in the stack while class objects are stored in the
heap. Then what happens when I add a struct value to a colection?

struct MyStruct
{
public int X;
}

MyStruct s;

ArrayList list = new ArrayList();
list.Add(s);

In C/C++ store stack addresses on a global collection can lead to hard to
trace problems. But how C# works? In this case what is stored on the list
collection: struct, a reference to the stack or s is cloned into the heap
and that reference is added to the collection?

Thanks in advance
--
Faustino Dina
--------------------------------------------------------
If my email address starts with two 'f'
drop the first 'f' when mailing me.

--
Faustino Dina
--------------------------------------------------------
If my email address starts with two 'f'
drop the first 'f' when mailing me.
Nov 16 '05 #1
2 1861
Instances of structs are not always stored on the stack -- rather struct
variables always directly contain their value. In your example, the struct
instances are boxed and allocated on the heap.
See http://www.yoda.arachsys.com/csharp/memory.html
--
Mickey Williams
Author, "Visual C# .NET Core Ref", MS Press
www.neudesic.com
www.servergeek.com

"faustino Dina" <ff****@matusa.com.mx> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a question about how C# handles struct. From the help, struct
variables are stored in the stack while class objects are stored in the
heap. Then what happens when I add a struct value to a colection?

struct MyStruct
{
public int X;
}

MyStruct s;

ArrayList list = new ArrayList();
list.Add(s);

In C/C++ store stack addresses on a global collection can lead to hard to
trace problems. But how C# works? In this case what is stored on the list
collection: struct, a reference to the stack or s is cloned into the heap
and that reference is added to the collection?

Thanks in advance
--
Faustino Dina
--------------------------------------------------------
If my email address starts with two 'f'
drop the first 'f' when mailing me.

--
Faustino Dina
--------------------------------------------------------
If my email address starts with two 'f'
drop the first 'f' when mailing me.

Nov 16 '05 #2
faustino Dina <ff****@matusa.com.mx> wrote:
I have a question about how C# handles struct. From the help, struct
variables are stored in the stack while class objects are stored in the
heap.
Unfortunately that's a rather simplistic approach.

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

(For instance, a value type which is part of a reference type will
always be on the heap.)
Then what happens when I add a struct value to a colection?

struct MyStruct
{
public int X;
}

MyStruct s;

ArrayList list = new ArrayList();
list.Add(s);
This is actually a third case - here you're *boxing* the struct value.
This basically creates a strongly-typed object on the heap, containing
your value. To get it back as a value type, you need to unbox the
reference, which you do using casting syntax:

s = (MyStruct) list[0];
In C/C++ store stack addresses on a global collection can lead to hard to
trace problems. But how C# works? In this case what is stored on the list
collection: struct, a reference to the stack or s is cloned into the heap
and that reference is added to the collection?


Essentially the latter.

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

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

Similar topics

26
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
13
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...
4
by: veera sekhar kota | last post by:
I read structs are stored at stack area or inline-heap based objects. What is meant by inline-heap based objects? I didnt get that. Thanks, Veera.
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.