-3

I want to know if it is possible to make an array of pointers to some arrays in C#. I want to do it to overcome the problem of getting OutOfMemoryException while allocating one very large object. I know that I can do it in C like here: Array of pointers to arrays. Can I do it in C#, and if it is possible, how to do it?

It is my method where i get exception:

private async Task FindNumbersDivisibleBySomeNumber()
{
    await Task.Run(() =>
    {
        for (long i = 1; i < 10000000000; i++)
        {
            if (i % someNumber == 0)
            {
                numbers.Add(i);
            }
        }
    });
}

Where numbers is a List of longs

Community
  • 1
  • 1
lukaszg
  • 23
  • 5
  • 1
    Show your code, there's probably a better solution, have you also allowed large objects in the app/web.config? – Lloyd Mar 20 '17 at 11:56
  • 2
    I guess the equivalent would be a jagged array, but I don't think that will fix your memory issue. – juharr Mar 20 '17 at 11:57
  • 2
    Array is a reference type (`class`, not `struct`) so I doubt if you want *poniters* (which are *unsafe*) in the context. – Dmitry Bychenko Mar 20 '17 at 11:57
  • Added a code of my method – lukaszg Mar 20 '17 at 12:08
  • C# normally allocates 8 bytes for a long, this results in roughly 74 GB of data out of the loop. I do not know your task, but you should probably try to split up the whole task somehow. – Thulur Mar 20 '17 at 12:14
  • 3
    WTF? Finding numbers that are divisible by x is simply calculated as x*i, going through the loop and checking division is just insane. – Zizy Archer Mar 20 '17 at 12:17
  • As for your question, you can hit 2 limitations. Stack size and amount of RAM. Stack can be overcome by stuff like pointers to pointers, but amount of RAM requires paging on the disk. Pointers to pointers won't help here - you will be using filenames where data is on the disk if you want to do stuff manually. – Zizy Archer Mar 20 '17 at 12:22
  • You start with a 1?? – Mark Peters Mar 20 '17 at 12:37
  • I know that my method could look strange but I just wanted to write a simple application where I can train using async and await keywords and see difference in time while using normal for loop and Parallel.For method. It is the only purpose of the app. – lukaszg Mar 20 '17 at 12:45

1 Answers1

0

Try following :

       static void Main(string[] args)
        {
            int[] t1={0,1,2,3};
            int[] t2={4,5,6,7};
            int[] t3={8,9,10,11};
            int[] t4={12,13,14,15};
            int[][] tab={t1,t2,t3,t4};
            for(int i=0; i<4;i++)
            {
                Console.WriteLine(string.Join(",", tab[i]));
            }

        }
jdweng
  • 33,250
  • 2
  • 15
  • 20