www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to use large size array, which are shared

reply "Sparsh Mittal" <sparsh0mittal gmail.com> writes:
Hello

I read: "The total size of a static array cannot exceed 16Mb. A
dynamic array should be used instead for such large arrays."

I want to make array which is shared but also has a large size,
e.g.

       shared WorkerClass[numberOfWorkers] myWorkerArray;

where numberOfWorkers is large.

If I try to do:

       shared WorkerClass[] myWorkerArray;
       int main()
       {
         .....
         myWorkerArray = new WorkerClass [numberOfWorkers];
       }

the compiler does not allow me to do so.

My motivation is to create a global array, which I can use in
multiple functions which are created using "spawn".

Can you please help me.
Jan 27 2013
next sibling parent "Sparsh Mittal" <sparsh0mittal gmail.com> writes:
Solved! I did:

myWorkerArray = new shared WorkerClass [numberOfWorkers];

Thanks.
Jan 27 2013
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/27/2013 01:44 PM, Sparsh Mittal wrote:
 Hello

 I read: "The total size of a static array cannot exceed 16Mb. A
 dynamic array should be used instead for such large arrays."

 I want to make array which is shared but also has a large size,
 e.g.

 shared WorkerClass[numberOfWorkers] myWorkerArray;

 where numberOfWorkers is large.

 If I try to do:

 shared WorkerClass[] myWorkerArray;
 int main()
 {
 .....
 myWorkerArray = new WorkerClass [numberOfWorkers];

shared is a part of the type: myWorkerArray = new shared(WorkerClass)[numberOfWorkers]; Alternatively, you can initialize myWorkerArray in a 'static this' block: class WorkerClass {} enum size_t numberOfWorkers = 10; shared WorkerClass[] myWorkerArray; static this() { myWorkerArray = new shared(WorkerClass)[numberOfWorkers]; } void main() {}
 Can you please help me.

Please also consider the D.learn newsgroup. Such threads are very helpful on that newsgroup as well. :) Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Jan 27 2013
prev sibling next sibling parent "Sparsh Mittal" <sparsh0mittal gmail.com> writes:
Thanks a lot. Sure, I post such questions on that forum.
Jan 27 2013
prev sibling parent "jerro" <a a.com> writes:
 shared WorkerClass[] myWorkerArray;

 static this()
 {
     myWorkerArray = new shared(WorkerClass)[numberOfWorkers];
 }

This should probably be "shared static this()". I'm guessing one would want this to run once before main(), not every time a thread is started.
Jan 27 2013