473,395 Members | 1,692 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,395 software developers and data experts.

heap or stack?

I'm develpoing a mapping app and want to store the point data that
makes up lines, points and polygons efficiently.

Am I better off using unsafe C# and creating structs to hold the data
on the stack (100K plus points total) or create classes for all the
data and put up with the overhead of boxing/unboxing?

Is there some middle ground - perhaps an array of structs containing
point data to represent a line wrapped in a class? - if I do that
will the data still be on the stack or will putting it in an array or
class make it an object and force it onto the heap.

Performance is a big factor.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #1
4 3133
MattAhsworth <Ma**********@hotmail-dot-com.no-spam.invalid> wrote:
I'm develpoing a mapping app and want to store the point data that
makes up lines, points and polygons efficiently.

Am I better off using unsafe C# and creating structs to hold the data
on the stack (100K plus points total) or create classes for all the
data and put up with the overhead of boxing/unboxing?

Is there some middle ground - perhaps an array of structs containing
point data to represent a line wrapped in a class? - if I do that
will the data still be on the stack or will putting it in an array or
class make it an object and force it onto the heap.

Performance is a big factor.


You don't need unsafe code to create structs, and you don't get boxing
if you just use classes. Could you try explaining *exactly* what you
believe your two options are?

Note that if you have an array of structs, that array will be on the
heap but there'll be no boxing going on.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi Matt,
Speaking of Points, Lines, Rectangles and Polygons. Here is what I found
based on my experience.
Points and Rectangles should be *structures*
Lines and polygons should be *classes*

Why?
Because points and rectangles are like primitive types for any graphics. A
lot of temporary objects of them are created passed as a function arguments
mostly by value, copy between each other and. arithmetic-like operation +/-
may make sense for them. That's all calls for *structure*. Otherwise you
will pollute the heap with small chunks of garbage.
At the other hand the very often reside in collections and as a fields of
classes, which may makes you think that it might be better to make them
classes.
Keep in mind that when value types are fileds of classes there is no boxing.
Structure's data is part of the objects data in the managed heap. About
collections... Don't forged that when value type objects are kept in array
they are in unboxed state so you don't have to worry about boxing/unboxing.
Thus, you can make your own strongly-typed collection classes and use
internaly arrays as a storage. This will solve your problem with
boxing/unboxing.

Lines and polygons at the other hand may be very big and heavy objects
usually you don create them as a local variables and don't pass them
by-value. Classes, I believe, are more appropriate for them.

--
HTH
B\rgds
100 [C# MVP]

"MattAhsworth" <Ma**********@hotmail-dot-com.no-spam.invalid> wrote in
message news:40********@Usenet.com...
I'm develpoing a mapping app and want to store the point data that
makes up lines, points and polygons efficiently.

Am I better off using unsafe C# and creating structs to hold the data
on the stack (100K plus points total) or create classes for all the
data and put up with the overhead of boxing/unboxing?

Is there some middle ground - perhaps an array of structs containing
point data to represent a line wrapped in a class? - if I do that
will the data still be on the stack or will putting it in an array or
class make it an object and force it onto the heap.

Performance is a big factor.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 15 '05 #3
Thanks for the replies. (not used forums before in 6 years of VB
programming and am very impressed at the speed and quality of
response).

Going to use array of structs as a field of each class of the
geographic objects. This way the data is held as value type on the
heap and I can let the garbage collector worry about memory without
performance coming into it too much.

Next question is then which array type? I think an arraylist, as long
as I set the size at creation, would be fastest for forwards/read
only access, is this true :?:
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #4
MattAhsworth <Ma**********@hotmail-dot-com.no-spam.invalid> wrote:
Thanks for the replies. (not used forums before in 6 years of VB
programming and am very impressed at the speed and quality of
response).

Going to use array of structs as a field of each class of the
geographic objects. This way the data is held as value type on the
heap and I can let the garbage collector worry about memory without
performance coming into it too much.

Next question is then which array type? I think an arraylist, as long
as I set the size at creation, would be fastest for forwards/read
only access, is this true :?:


ArrayList isn't an array. If you use ArrayList, you'll end up with
boxing. If you know the size at creation time, use a plain array
instead, as that will avoid boxing and be very fast.

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

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

Similar topics

14
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...
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; };
1
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...
2
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...
3
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
9
by: shine | last post by:
what is the difference between a heap and a stack?
24
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...
16
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...
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...
9
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...

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.