473,503 Members | 4,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Failed to initialize huge arrays

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

Oct 12 '05 #1
3 2108
<dp*****@gmail.com> wrote:
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!

--
Jon Skeet - <sk***@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
Oct 12 '05 #2
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);
}
}

Oct 13 '05 #3
<dp*****@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@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
Oct 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
9076
by: SamMan | last post by:
Is there an easy way to re-initialize an array, other than looping through it's length and setting the values to null, or ""? Would unset() be a wise choice? Thanks again -- SamMan
7
46414
by: Andi.Martin | last post by:
Hi, how is it possible, to only initialize parts of a structure. Example: typedef struct{ int a, b; ... (huge lot of members); double x,y;
3
11783
by: Gustavo L. Fabro | last post by:
Greetings! I'm testing VS 2005 Beta 2, and I've compiled a program that runs just fine on my computer (where VS is installed). I've tried to run this same program on another computer (after...
6
2445
by: Michael Gray | last post by:
VS 2003 VB.net Win2000 SP4 The System.Array class seems to be limited to 32 bit addresses, meaning that one can only assign 2^32 elements. Is there any way that I can have an array that...
3
412
by: dpriver | last post by:
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...
5
1798
by: Peter Steiner | last post by:
I have written a Service in C++ (VC8) derived from System::ServiceProcess::ServiceBase. In the OnStart() method I detect a service failure but the SCM reports that the service started...
5
12506
by: Benny Raymond | last post by:
Basically what the subject says... "in a bool 2d array, does Initialize set everything to false?"
2
6176
by: deko | last post by:
I trying to create a jagged array of two arrays, with the second array being an array of two-dimensional arrays. A graphical representation might look like this: x y y y x y x y y x ...
0
2579
by: Jason | last post by:
I am attempting to have an asp page call a batch file. I had it working, I was high on life and then I decided to log off the server. Once I did, the code would no longer work. When I logged back...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.