January 15, 2004 Re: Have to use class where struct would suffice? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede |
> 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
|
January 15, 2004 Re: Have to use class where struct would suffice? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ssuukk | 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.
>>
> 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.
>
wow i know alot more about stacks and heaps now, thanks for explaining so clearly everyone...
regards
Lewis
|
Copyright © 1999-2021 by the D Language Foundation