Failed to initialize huge arrays

  • Thread starter Thread starter dpriver
  • Start date Start date
D

dpriver

I have to initialize a huge constant array of structures(60000~100000),
used as a lookup table , but it failed with
System.InvalidProgramException
when the items reach 18400. and if I change the item type from struct
to class,
the threshold is 35000 . It seems that there a size limition when
initialize constant in CLR.
Is there any configurable parameters can be used to increase this
limition in C# compiler
or .NET framework. Or I'm missing something there?




using System;

struct st
{
public short t1,t2 ;
public st(short t1,short t2){
this.sym = t1;
this.act = t2;
}
}

class Test{
static void Main(){

st[] a = {
new st(3,4),
new st(3,4),
new st(3,4),
new st(3,4),
new st(3,4),
new st(3,4),
new st(1,2),
...
...
repeated more than 20000 times
...
...
...
new st(3,4),
new st(1,2),
new st(1,2),
new st(3,4),
new st(1,2),
new st(1,2),
new st(3,4)
};
Console.WriteLine("{0}",a[1].t1);
}
}
 
I have to initialize a huge constant array of structures(60000~100000),
used as a lookup table , but it failed with
System.InvalidProgramException
when the items reach 18400. and if I change the item type from struct
to class,
the threshold is 35000 . It seems that there a size limition when
initialize constant in CLR.
Is there any configurable parameters can be used to increase this
limition in C# compiler
or .NET framework. Or I'm missing something there?

This really isn't the best way of accomplishing your task. It would be
much better to load the data from a stream (whether an embedded
resource or an external file). That way you won't run into this limit,
and your code will be a lot simpler too!
 
Thanks for your help.
It seems a good solution to use embedded resource file to manage this
big arrays.

But there is another problem when getting back this array back from
resource file.

there is always a System.InvalidCastException for this line:

st[] a = (st[]) rm.GetObject("array 1",ci);

can anyone help?


It's ok to create a resource file using following code:


using System;
using System.Resources;


[Serializable]
struct st
{
public short t1,t2 ;
public st(short t1,short t2){
this.t1 = t1;
this.t2 = t2;
}
}

public class WriteResources {

public static void Main(string[] args) {

st[] a = {
new st(3,4),
new st(3,4),
new st(3,4),
new st(3,4),
new st(3,4),
new st(3,4)
};


// Creates a resource writer.
IResourceWriter writer = new
ResourceWriter("myResources.resources");

// Adds resources to the resource writer.
writer.AddResource("String 1", "First String");

writer.AddResource("array 1", a);

writer.Generate();

// Writes the resources to the file or stream, and closes it.
writer.Close();
}
}

but failed when retrieve array back from the resource file,

using System;
using System.Resources;
using System.Collections;
using System.Globalization;
using System.Threading;
using System.Reflection;

[Serializable]
struct st
{
public short t1,t2 ;
public st(short t1,short t2){
this.t1 = t1;
this.t2 = t2;
}
}

class EnumerateResources
{


public static void Main()
{

// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager("myResources",
Assembly.GetExecutingAssembly());

CultureInfo ci = Thread.CurrentThread.CurrentCulture;

//It's ok to retrieve string
String str = rm.GetString("String 1", ci);
Console.WriteLine(str);

//THERE IS ALWAYS A System.InvalidCastException for this line:
st[] a = (st[]) rm.GetObject("array 1",ci);
Console.WriteLine(a[1].t1);
}
}
 
It seems a good solution to use embedded resource file to manage this
big arrays.

But there is another problem when getting back this array back from
resource file.

there is always a System.InvalidCastException for this line:

st[] a = (st[]) rm.GetObject("array 1",ci);

can anyone help?

It's ok to create a resource file using following code:

Well, I can't say I've used the ResourceManager much myself. I was
suggesting using it just as an embedded file, getting the stream from
the assembly and then reading it in manually. Hopefully someone else
will have more experience with ResourceManager though.
 
Back
Top