Connecting Tech Pros Worldwide Forums | Help | Site Map

Failed to initialize huge arrays

dpriver@gmail.com
Guest
 
Posts: n/a
#1: Oct 12 '05
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);
}
}


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Oct 12 '05

re: Failed to initialize huge arrays


<dpriver@gmail.com> wrote:[color=blue]
> 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?[/color]

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!

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
dpriver@gmail.com
Guest
 
Posts: n/a
#3: Oct 13 '05

re: Failed to initialize huge arrays


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);
}
}

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Oct 13 '05

re: Failed to initialize huge arrays


<dpriver@gmail.com> wrote:[color=blue]
> 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:[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Closed Thread


Similar .NET Framework bytes