Jump to page: 1 2
Thread overview
Have to use class where struct would suffice?
Jan 14, 2004
Georg Wrede
Jan 14, 2004
davepermen
Jan 14, 2004
C
Jan 15, 2004
Lewis
Jan 15, 2004
J Anderson
Jan 15, 2004
Andy Friesen
Jan 15, 2004
ssuukk
Jan 15, 2004
Lewis
Jan 15, 2004
Ben Hinkle
Jan 15, 2004
Georg Wrede
Jan 15, 2004
Ben Hinkle
Jan 15, 2004
ssuukk
January 14, 2004
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.


January 14, 2004
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.
>
>


January 14, 2004
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.
>
>


January 15, 2004
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
January 15, 2004
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

January 15, 2004
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
January 15, 2004
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.
>
>


January 15, 2004
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.
> 
Even shorter version:

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.

January 15, 2004
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.
> 
Well - actually I am not sure that difference between class and struct (heap vs stack) is mentioned somewhere in the manual. I think I checked it but didn't find anything...

January 15, 2004

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.
>>
>>
>
>


« First   ‹ Prev
1 2