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

Class or structure for array.

Hello,

if you have an array with some class as element type and a same sized array
with some structure as element type, and that class and structure have the
same fields (for example 10 integers), is it correct to assume that the
array with the classes uses less memory (than the array with structures)
before the objects are initialized (X bytes per object) and uses more memory
after all the objects are initialized (same fields + X bytes per object) ?

Thanks.

Dec 16 '05 #1
14 1416
"Qwert" <no**@nosp.com> schrieb:
if you have an array with some class as element type and a same sized
array with some structure as element type, and that class and structure
have the same fields (for example 10 integers), is it correct to assume
that the array with the classes uses less memory (than the array with
structures) before the objects are initialized (X bytes per object) and
uses more memory after all the objects are initialized (same fields + X
bytes per object) ?


An array of structures uses more memory than an array of class instances
whose items point to 'Nothing'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 16 '05 #2
hi Qwert,
i'm agree with Herfried. structure is a user definded data type just
like class but the difference is structure doesnt required to be
instanciated. thus structure objects are maintaining references on
stack memory unlike classes those maintaines references in heap memory.
stack memory is limited in comparison of heap memory so that structures
occupies more memory in stack when they get references. that's why
structure is quite costlier than class.

there are more reasons related to memory but to cut the story in short,
structure eats lots of memory when they get instanciated.

Dec 16 '05 #3
thus structure objects are maintaining references on stack memory
I'm not sure exactly what you mean by that, but there are no
references involved for value types (Structures). And when you have an
array of the structure the elements are heap allocated inline in the
array object, not on the stack.

structure eats lots of memory when they get instanciated.


They "eat" exactly the size of their members (plus padding for
alignment), no more and no less. Compared to classes where you have a
small per-object overhead, they are quite cheap.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 16 '05 #4
Mattias,
thus structure objects are maintaining references on stack memory


to stack memory,

I have not in any way a problem with the simple way Lucky describes it, is
it in fact not true than it is in my opinion in sense true to show what is
the difference.

If you have a car that uses petrol, than the motor is not going on petrol,
than it is on air mixed with vaporized petrol. You will probably never say
that.

Just my opinion,

Cor
Dec 16 '05 #5

Structures are also much faster to access
here is some more info regarding structures vs classes
the official reading materiall

http://msdn.microsoft.com/library/de...andclasses.asp

and a verry clear explanation

http://www.devcity.net/Articles/79/practical_oop_1.aspx
regards

Michel Posseth [MCP]
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Od*************@TK2MSFTNGP11.phx.gbl...
thus structure objects are maintaining references on stack memory


I'm not sure exactly what you mean by that, but there are no
references involved for value types (Structures). And when you have an
array of the structure the elements are heap allocated inline in the
array object, not on the stack.

structure eats lots of memory when they get instanciated.


They "eat" exactly the size of their members (plus padding for
alignment), no more and no less. Compared to classes where you have a
small per-object overhead, they are quite cheap.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Dec 16 '05 #6
Michel,
Structures are also much faster to access

That is as long as there is programming.

Everything direct to reference on stack zero (or used stack) is the much
fast to use.

Cor
Dec 16 '05 #7
most or whatever

:-)

Cor
Dec 16 '05 #8
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb
Michel,
Structures are also much faster to access

That is as long as there is programming.

Everything direct to reference on stack zero (or used stack) is the
much fast to use.


Cor, what is stack zero or used stack?
Armin
Dec 16 '05 #9
Armin,

Just a freedom of speech, I mean the main part of the program when you look
at the located memory.

However it can be of course forever for any stack after that.

Main(0)
Stack(1)
Stack(1,1)
Stack(2)
End

However be aware that we here are talking about thousends of nanoseconds.

Cor
Dec 17 '05 #10
Thank you all.

"Lucky" <tu************@gmail.com> schreef in bericht
news:11*********************@f14g2000cwb.googlegro ups.com...
hi Qwert,
i'm agree with Herfried. structure is a user definded data type just
like class but the difference is structure doesnt required to be
instanciated. thus structure objects are maintaining references on
stack memory unlike classes those maintaines references in heap memory.
stack memory is limited in comparison of heap memory so that structures
occupies more memory in stack when they get references. that's why
structure is quite costlier than class.

there are more reasons related to memory but to cut the story in short,
structure eats lots of memory when they get instanciated.

Dec 17 '05 #11
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb
Armin,

Just a freedom of speech, I mean the main part of the program when
you look at the located memory.

However it can be of course forever for any stack after that.

Main(0)
Stack(1)
Stack(1,1)
Stack(2)
End

However be aware that we here are talking about thousends of
nanoseconds.

What is this above? Maybe there is something I don't know. I only know that
each thread has it's stack linearily being filled from end to start.
Armin

Dec 17 '05 #12
Armin,

I can be as well that I misinterpret it, however for me the working from a
stack has always been this

Main,
Call method1
means stack(1) is opened
In stack 1 is callex method x
means stack (1.2) is opened, if 1.2 exits it returns in (1)
at the next program addres (stack 1.2 becomes unreachable)
If stack 1 ends than it goes on tho the next program address in
main.

This has nothing to do with the way a program is written by the way the
methods can be called everywhere.

However if it is not done in this way in Net than it is my misunderstanding.
As you probably always have seen, I am not so much interested in *how* the
program acts under the hood. It cost tons of time and you gain in my opinion
mostly nothing with it. I assume that those who did their job doing this for
us, did the right job. It is bad enough if you have to go deeper because of
a lack of documentation.

Just my opinion about that of course.

Cor
Dec 17 '05 #13
Cor,

the stack is a linear piece of memory. Every location on the stack can be
accessed directly. It's only an offset that has to be added when accessing a
specific item. The value that is added has no impact on the speed, just like
the time is the same if you access b(17) or b(50) in an array.
Armin

Dec 17 '05 #14
Armin,

the stack is a linear piece of memory. Every location on the stack can be
accessed directly. It's only an offset that has to be added when accessing
a specific item. The value that is added has no impact on the speed, just
like the time is the same if you access b(17) or b(50) in an array.


After that I had sent it, I was realising me that. That was why I wrote
thousands of nanoseconds to you in a previous message.

AFAIK cost adding zero to a register less time than adding a million (there
is not any overload to catch). However very very very very very less time.

:-)

Not needed to reply, you are right.

Cor

Dec 17 '05 #15

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

Similar topics

4
by: David Compton | last post by:
Hi all, Can anyone tell me if this is possible ? :- I have several flat files created in VB6 using UDTs and the PUT command. They include integers, etc. and are all of fixed record length. ...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
1
by: MikeCS | last post by:
Problem: I wanted to implement what I did in VB6 where I would use a global structure and dimension an array of structures then the program could load data into it at start up. Global routines would...
7
by: triplejump24 | last post by:
Hello. Im working on what seems to me a very complicated programming assignment. I basically have to design a video library using class. The program needs to read data files i have and put the into...
5
Banfa
by: Banfa | last post by:
I thought I would write a little article about this because it is regularly asked as a question in the Forums. Firstly this is a C++ issue, it is strictly against the rules of C to create a class...
1
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define...
4
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
1
SamKL
by: SamKL | last post by:
Hey, I'm no expert on PHP, and I have somewhat of an understanding of object oriented code. Anyway, getting right to the problem. I'm using PHP4, so base it off of that. Basically I have 2...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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: 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
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,...
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,...

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.