April 18, 2006 Re: just a few small questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus |
>I think you are best off using the heap for your large global array. Data placed on the heap stays there until you explicitly delete it, so you don't have to use a global. Heap is also what you use if you have any sort of dynamic memory needs, you can allocate what you need when you need it. If you use new/delete to much, the heap may become segmented and then the garbage collector will run (I think). The heap is unlimited for the most part, while you can only allocate so much data onto the stack. The heap might be slower to access, but not enough to be a significant problem.
>
>Maybe an expert can vindicate my post?
>~ Clay
Why is wrong with using a Global? When I need an array for the whole time and more than half of the subs need to access it, I just don't want to use those annoying pointers the whole time :).. how is this done in D?
|
April 19, 2006 Re: just a few small questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to MM | MM wrote:
>>I think you are best off using the heap for your large global array. Data placed on the heap stays there until you explicitly delete it, so you don't have to use a global. Heap is also what you use if you have any sort of dynamic memory needs, you can allocate what you need when you need it. If you use new/delete to much, the heap may become segmented and then the garbage collector will run (I think). The heap is unlimited for the most part, while you can only allocate so much data onto the stack. The heap might be slower to access, but not enough to be a significant problem.
>>
>>Maybe an expert can vindicate my post?
>>~ Clay
>
>
> Why is wrong with using a Global? When I need an array for the whole time and
> more than half of the subs need to access it, I just don't want to use those
> annoying pointers the whole time :).. how is this done in D?
>
Pointers, schmointers. You can pass an array as normal to a function without copying. (This is where the Copy-on-Write people might speak up. I'll let them.) Also, if you intend to replace the array during a function, just use an 'out' or 'inout' parameter.
Although personally I have no problem with using globals. :)
-- Chris Nicholson-Sauls
|
April 19, 2006 Re: just a few small questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to MM | "MM" <MM_member@pathlink.com> wrote in message news:e23tmv$1gaq$1@digitaldaemon.com... > Why is wrong with using a Global? When I need an array for the whole time > and > more than half of the subs need to access it, I just don't want to use > those > annoying pointers the whole time :).. how is this done in D? Well, if you're programming in a C-style fashion, where you just have a bunch of global data and a bunch of functions to manipulate it.. I suppose it's kind of _messy_ and certainly not my preferred method, but there's nothing stopping _you_ from doing it :) One of the problems of using a large global array is that it's allocated in the static data segment. This isn't a problem if you just define int[500][500] x; But if you initialize any of it statically: int[500][500] x = [0 : [0 : 1]]; (which means set x[0][0] = 1) It makes the EXE size jump up quite a bit. In this case, by about 1MB. You can get around this by using static module constructors though: int[500][500] x; static this() { x[0][0] = 1; } |
April 22, 2006 Re: just a few small questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Nicholson-Sauls | Chris Nicholson-Sauls wrote:
> MM wrote:
>>> I think you are best off using the heap for your large global array. Data placed on the heap stays there until you explicitly delete it, so you don't have to use a global. Heap is also what you use if you have any sort of dynamic memory needs, you can allocate what you need when you need it. If you use new/delete to much, the heap may become segmented and then the garbage collector will run (I think). The heap is unlimited for the most part, while you can only allocate so much data onto the stack. The heap might be slower to access, but not enough to be a significant problem.
>>>
>>> Maybe an expert can vindicate my post?
>>> ~ Clay
>>
>>
>> Why is wrong with using a Global? When I need an array for the whole time and
>> more than half of the subs need to access it, I just don't want to use those
>> annoying pointers the whole time :).. how is this done in D?
>>
>
> Pointers, schmointers. You can pass an array as normal to a function without copying. (This is where the Copy-on-Write people might speak up. I'll let them.) Also, if you intend to replace the array during a function, just use an 'out' or 'inout' parameter.
>
> Although personally I have no problem with using globals. :)
>
> -- Chris Nicholson-Sauls
if by replace your mean modify the array itself, no 'out' is needed since arrays are passed by reference, just like objects.
If you want to change the *variable* in the calling function, then an (in)out is needed.
-DavidM
|
Copyright © 1999-2021 by the D Language Foundation