Failed to initialize huge arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dpriver@gmail.com

    Failed to initialize huge arrays

    I have to initialize a huge constant array of structures(6000 0~100000),
    used as a lookup table , but it failed with
    System.InvalidP rogramException
    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.WriteLi ne("{0}",a[1].t1);
    }
    }

  • Jon Skeet [C# MVP]

    #2
    Re: Failed to initialize huge arrays

    <dpriver@gmail. com> wrote:[color=blue]
    > I have to initialize a huge constant array of structures(6000 0~100000),
    > used as a lookup table , but it failed with
    > System.InvalidP rogramException
    > 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.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • dpriver@gmail.com

      #3
      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.InvalidC astException for this line:

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

      can anyone help?


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


      using System;
      using System.Resource s;


      [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.re sources");

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

      writer.AddResou rce("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.Resource s;
      using System.Collecti ons;
      using System.Globaliz ation;
      using System.Threadin g;
      using System.Reflecti on;

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

      class EnumerateResour ces
      {


      public static void Main()
      {

      // Create a resource manager to retrieve resources.
      ResourceManager rm = new ResourceManager ("myResource s",
      Assembly.GetExe cutingAssembly( ));

      CultureInfo ci = Thread.CurrentT hread.CurrentCu lture;

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

      //THERE IS ALWAYS A System.InvalidC astException for this line:
      st[] a = (st[]) rm.GetObject("a rray 1",ci);
      Console.WriteLi ne(a[1].t1);
      }
      }

      Comment

      • Jon Skeet [C# MVP]

        #4
        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.InvalidC astException for this line:
        >
        > st[] a = (st[]) rm.GetObject("a rray 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.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...