Re: Memory (Structure vs Class)
- From: "Ralph" <nt_consulting64@xxxxxxxxx>
- Date: Wed, 29 Jul 2009 13:18:02 -0500
"David" <dw85745NOT@xxxxxxxxxxxxx> wrote in message
news:u0X0gXGEKHA.5092@xxxxxxxxxxxxxxxxxxxxxxx
As I recall, I believe classes grew out of structures in an attempt to
link the data with the variables that refer to or use the data.
I suppose you could put it that way. It is more useful however to consider
both as just 'programming constructs' to create a block of memory with
addressable (is that a word? lol) elements. Such a block of memory which is
treated as a single "thingy" is referred to as a "Type".
Bundling a bunch of related variables into one package and passing that
around instead of all the separate variables is obviously very convenient.
The WinAPI makes use of several structures. In order for VB to work with the
WinAPI and other published APIs - VB provides a construct to create such
blocks of memory by declaring UDTs. (They could have just called them
"structs", but since VB is an OOPL they opted for the more 'OO' term of
"User-defined Types".)
OOPLs introduced the concept of Objects and then respectively a programming
construct to create them. That construct is called a 'Class'. In
implementation (down at the dirty little level of 1s and ohs) there is no
difference between a struct and an object. The end result is a block of
memory with addressable elements.
Objects are often considered different because they can have methods. But
actually so can structs (Easier to accomplish in C than VB, but possible.)
The major difference is as Larry pointed out - creating objects (by using
the Class construct) makes it easier for a programmer to build OO solutions
(data hiding, polymorphism, etc.).
Also from a memory perspective (based on my internet reading for NET --
haven't found anything for VB Classic) that classes are referenced on the
heap while structures would be the stack unless declared global.
That is pretty much language and development platform dependent, and of
course context. In VB UDTs could be on the stack, in a data segment, or even
on the heap - all depends on where they are declared and or how they are
used.
In VB objects can only be declared on the heap. Other languages allow for
the same versatility as for structs.
I was always taught to go to lowest level -- hence stack would be better.
Whether just the class reference goes on the heap and the class itself is
handled on the stack is unknown (to me).
Whether any of this means a hoot with the amount of memory today also is
unknown.
In general, use the construct that makes the most logical sense for the
solution. "Pre-optimizing" while developing a program (other than simple
common sense) is a distraction and often a waste of time.
Most of the time, when you are using an OOPL and have identified a bundle of
associated 'values' - you usually have a Type and a Type usually means an
Object.
-ralph
.
- Follow-Ups:
- Re: Memory (Structure vs Class)
- From: David
- Re: Memory (Structure vs Class)
- References:
- Memory (Structure vs Class)
- From: David
- Re: Memory (Structure vs Class)
- From: Ralph
- Re: Memory (Structure vs Class)
- From: David
- Re: Memory (Structure vs Class)
- From: Larry Serflaten
- Re: Memory (Structure vs Class)
- From: David
- Memory (Structure vs Class)
- Prev by Date: Re: Calculating Median on array of Longs
- Next by Date: Re: Calculating Median on array of Longs
- Previous by thread: Re: Memory (Structure vs Class)
- Next by thread: Re: Memory (Structure vs Class)
- Index(es):
Relevant Pages
|