www.digitalmars.com         C & C++   DMDScript  

D - Have to use class where struct would suffice?

reply Georg Wrede <Georg_member pathlink.com> writes:
To begin with, I'm moving and out-of-internet right now, so 
I have to write this without my notes. Anyhow:

Recently I've run to some situations where I've thought it
natural to use structs. In some of these, changing the word
struct to class has done the thing. (So a class without my
constructors, etc., essentially just a struct, from my 
point of view.)

Appologies for not having specific examples here. I've read
through the entire D documentation, browsed src stuff, but
I'm now getting the impression there's something I'm missing.
Jan 14 2004
next sibling parent davepermen <davepermen_member pathlink.com> writes:
just use a struct then..

if you come from c++, remembeer, in c++, structs and classes are EXACTLY THE
SAME. only the default public/private setting differs.

In article <bu4j07$2m7d$1 digitaldaemon.com>, Georg Wrede says...
To begin with, I'm moving and out-of-internet right now, so 
I have to write this without my notes. Anyhow:

Recently I've run to some situations where I've thought it
natural to use structs. In some of these, changing the word
struct to class has done the thing. (So a class without my
constructors, etc., essentially just a struct, from my 
point of view.)

Appologies for not having specific examples here. I've read
through the entire D documentation, browsed src stuff, but
I'm now getting the impression there's something I'm missing.

Jan 14 2004
prev sibling next sibling parent reply "C" <dont respond.com> writes:
Also in D , structs are create on the stack , classes ( unless auto'd ) are
usually new'd on the heap.

C

"Georg Wrede" <Georg_member pathlink.com> wrote in message
news:bu4j07$2m7d$1 digitaldaemon.com...
 To begin with, I'm moving and out-of-internet right now, so
 I have to write this without my notes. Anyhow:

 Recently I've run to some situations where I've thought it
 natural to use structs. In some of these, changing the word
 struct to class has done the thing. (So a class without my
 constructors, etc., essentially just a struct, from my
 point of view.)

 Appologies for not having specific examples here. I've read
 through the entire D documentation, browsed src stuff, but
 I'm now getting the impression there's something I'm missing.

Jan 14 2004
parent reply Lewis <dethbomb hotmail.com> writes:
C wrote:

 Also in D , structs are create on the stack , classes ( unless auto'd ) are
 usually new'd on the heap.
 

what is the difference (in newbie terms) between the stack and the heap? thanks
Jan 14 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Lewis wrote:

 C wrote:

 Also in D , structs are create on the stack , classes ( unless auto'd 
 ) are
 usually new'd on the heap.

what is the difference (in newbie terms) between the stack and the heap? thanks

Stack = faster (generally -> depends how you use it). Heap = slower (generally -> depends how you use it). The stack keeps putting things on top of a single pile. Like a pile of dishes. ie void test2() { int y; } void test() { int x; //On the stack test2(); } void main() { test(); } First the program calls test() x is put on the stack so we have: x -> Stack bottom (and top) Then it calls test2 so y is put on the stack y x -> Stack bottom Then test2 ends, y is taken of the stack. x -> Stack bottom (and top) Then test ends -> nothing on the stack Note that's a rather simplisic view (memory has different sizes). Note that many parameters are copied on the stack just before the function is run. Problem with stack? You can't have dynamicly sizable memory (ie resizable arrays). The heap, is like tetris. The program tries to find free blocks of memory to store your memory. ie if you want 4 integers, it will look for 32-bits*4 of free space that are in a row (in the large block of memory). As you can imagine, this can be a time consuming process (dw: it's still reasonably fast because of the types of algorithms used). XXXXXXX____XX___XXX___ Memory looks something like the above, only there's allot more (looks kinda like defrag if you've used that). ie void create(int size) //Note that size is on the stack { int array[size]; //search for a row of size*32 to allocate from the heap } When you allocate a normal class, it goes on the heap. This is so it can be removed (Freed) whenever you wish (which is another problem with the stack). However, the actual pointer goes on the stack, but a pointers only 32-bits where each member on a class can be around 32-bits (translation -> classes can take up heaps of memory). D takes care of the removal process with the GC so you generally don't need to worry about deleting your objects. It's deleted when it's no longer being used (which is a difference from C++ but java also has a GC). If you allocate a struct instance or a auto object then you don't get a pointer on the stack, you get the entire class pushed on to the stack (ie each member in order goes on to the stack). I hope that's simple enough. Anderson
Jan 14 2004
parent reply Andy Friesen <andy ikagames.com> writes:
J Anderson wrote:
 [....]

Shorter version: Stack allocated memory only lasts until the end of the function. Heap storage sticks around as long as you need it to. (only heap things need garbage collection, so only heap things are garbage collected) Stack things can also be global; they exist for the duration of the program. -- andy
Jan 14 2004
parent reply ssuukk <ssuukk .go2.pl> writes:
Andy Friesen wrote:

 Shorter version:
 
 Stack allocated memory only lasts until the end of the function.  Heap 
 storage sticks around as long as you need it to. (only heap things need 
 garbage collection, so only heap things are garbage collected)
 
 Stack things can also be global; they exist for the duration of the 
 program.
 

Imagine you're writing a game and decide to implement your own vector/matrix/quaternion classes. Since you want them to work exactly same as int/double/char and so on (without using new and delete) you have to make them operate with stack (struct). And when you allocate an object dynamically (like an array, or dynamically generated terrain) it needs to go to heap by the use of new/delete.
Jan 15 2004
parent Lewis <dethbomb hotmail.com> writes:
ssuukk wrote:
 Andy Friesen wrote:
 
 Shorter version:

 Stack allocated memory only lasts until the end of the function.  Heap 
 storage sticks around as long as you need it to. (only heap things 
 need garbage collection, so only heap things are garbage collected)

 Stack things can also be global; they exist for the duration of the 
 program.

Imagine you're writing a game and decide to implement your own vector/matrix/quaternion classes. Since you want them to work exactly same as int/double/char and so on (without using new and delete) you have to make them operate with stack (struct). And when you allocate an object dynamically (like an array, or dynamically generated terrain) it needs to go to heap by the use of new/delete.

wow i know alot more about stacks and heaps now, thanks for explaining so clearly everyone... regards Lewis
Jan 15 2004
prev sibling next sibling parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
Structs have value semantics and class instances have reference semantics.
Meaning if T is a struct and x and y have type T then
 x = y;
copies the contents of y into x (like C). If T is a class then it would have
x refer to the same object and it would share y's contents (like Java/C# but
not C++).
-Ben

"Georg Wrede" <Georg_member pathlink.com> wrote in message
news:bu4j07$2m7d$1 digitaldaemon.com...
 To begin with, I'm moving and out-of-internet right now, so
 I have to write this without my notes. Anyhow:

 Recently I've run to some situations where I've thought it
 natural to use structs. In some of these, changing the word
 struct to class has done the thing. (So a class without my
 constructors, etc., essentially just a struct, from my
 point of view.)

 Appologies for not having specific examples here. I've read
 through the entire D documentation, browsed src stuff, but
 I'm now getting the impression there's something I'm missing.

Jan 14 2004
parent reply Georg Wrede <Georg_member pathlink.com> writes:
In article <bu55bg$gts$1 digitaldaemon.com>, Ben Hinkle says...
Structs have value semantics and class instances have reference semantics.
Meaning if T is a struct and x and y have type T then
 x = y;
copies the contents of y into x (like C). If T is a class then it would have
x refer to the same object and it would share y's contents (like Java/C# but
not C++).
-Ben

Whoops, that's a major difference! Somehow I've entirely missed that one. This explains a lot. Maybe this should be more prominently mentioned in the docs?
"Georg Wrede" <Georg_member pathlink.com> wrote in message
news:bu4j07$2m7d$1 digitaldaemon.com...
 To begin with, I'm moving and out-of-internet right now, so
 I have to write this without my notes. Anyhow:

 Recently I've run to some situations where I've thought it
 natural to use structs. In some of these, changing the word
 struct to class has done the thing. (So a class without my
 constructors, etc., essentially just a struct, from my
 point of view.)

 Appologies for not having specific examples here. I've read
 through the entire D documentation, browsed src stuff, but
 I'm now getting the impression there's something I'm missing.


Jan 15 2004
parent "Ben Hinkle" <bhinkle4 juno.com> writes:
 Maybe this should be more prominently mentioned in the docs?

Yeah, I agree. I looked in the sections about structs, classes and the meaning of "=" and I couldn't find any mention of how assignment works. Also for arrays it says if a and b are dynamic arrays then a = b; a points to the same array as b does I think that is somewhat misleading since the statement b.length = b.length+1; will not resize "a". So the statement "points to the same array" only makes sense if the length property is not considered part of the array, which seems like a wierd concept. Sometimes "array" refers to just the data and sometimes it refers to both the length and the data. At first I thought array assignment shared the length property until I started programming in D and it was obvious that wasn't quite right. Then it took a while to realize what array assignment was really doing. -Ben
Jan 15 2004
prev sibling parent ssuukk <ssuukk .go2.pl> writes:
Georg Wrede wrote:

 To begin with, I'm moving and out-of-internet right now, so 
 I have to write this without my notes. Anyhow:
 
 Recently I've run to some situations where I've thought it
 natural to use structs. In some of these, changing the word
 struct to class has done the thing. (So a class without my
 constructors, etc., essentially just a struct, from my 
 point of view.)
 
 Appologies for not having specific examples here. I've read
 through the entire D documentation, browsed src stuff, but
 I'm now getting the impression there's something I'm missing.
 

(heap vs stack) is mentioned somewhere in the manual. I think I checked it but didn't find anything...
Jan 15 2004