Re: What is the maximum acceptable size of an array?

From: Victor Perevoztchikov <perev_at_bnl.gov>
Date: Mon, 9 Jul 2007 12:30:44 -0400


Hi John,
sure it is related to stack size. When it is not compiled, then this array created by new. If you will do instead of
  char a[MAX];
char *a = new char[MAX];
and at the end delete [] a
it will work allways correctly
Victor

 Victor M. Perevoztchikov perev_at_bnl.gov Brookhaven National Laboratory MS 510A PO Box 5000 Upton NY 11973-5000 tel office : 631-344-7894; fax 631-344-4206;

  I've encountered a strange problem in one of my macros. I managed to reduce it to an array size problem.

  This macro runs without errors when compiled (.L toto.C+) with "#define MAX 1972432" and exits root without any message if compiled with "#define MAX 1972433":
//includes

  #include <iostream>
  using namespace std;

//OK

  #define MAX 1972432
//not OK
//#define MAX 1972433

//main function

  int toto()
  {
    char a[MAX];     

    cout<<"MAX="<<MAX<<endl;
    cout<<"total size="<<MAX*sizeof(char)<<" bytes"<<endl;
    cout<<"total size="<<(float)MAX*(float)sizeof(char)/1024.<<" Kilobytes"<<endl;
    cout<<"total size="<<(float)MAX*(float)sizeof(char)/(1024.*1024.)<<" Megabytes"<<endl;
    cout<<"total size="<<MAX*sizeof(char)*8<<" bits"<<endl;
    

    return(0);
  }

  You might have to change the limit values of MAX to see this problem on your PC.   I noticed that it's quite random sometimes. Sometimes it worked with 1972432 and sometimes not.   I also recommend quitting and restarting ROOT after each test because it seems to always work if you stay in the ROOT shell even after recompiling.

  Other tests I made:

  This macro runs without error when not compiled (.x toto2.C):
//includes

  #include <iostream>
  using namespace std;

//OK

  #define MAX 10000000
//not OK
//#define MAX 1972433

//main function

  int toto2()
  {
    char a[MAX];     

    cout<<"MAX="<<MAX<<endl;
    cout<<"total size="<<MAX*sizeof(char)<<" bytes"<<endl;
    cout<<"total size="<<(float)MAX*(float)sizeof(char)/1024.<<" Kilobytes"<<endl;
    cout<<"total size="<<(float)MAX*(float)sizeof(char)/(1024.*1024.)<<" Megabytes"<<endl;
    cout<<"total size="<<MAX*sizeof(char)*8<<" bits"<<endl;
    

    return(0);
  }

  This C++ program compiles and runs without error:
//includes

  #include <iostream>
  using namespace std;

//OK

  #define MAX 10000000
//not OK
//#define MAX 1972433

//main function

  int main()
  {
    char a[MAX];     

    cout<<"MAX="<<MAX<<endl;
    cout<<"total size="<<MAX*sizeof(char)<<" bytes"<<endl;
    cout<<"total size="<<(float)MAX*(float)sizeof(char)/1024.<<" Kilobytes"<<endl;
    cout<<"total size="<<(float)MAX*(float)sizeof(char)/(1024.*1024.)<<" Megabytes"<<endl;
    cout<<"total size="<<MAX*sizeof(char)*8<<" bits"<<endl;
    

    return(0);
  }

  So, is there any rational explanation to this?   Should I use TCloneArrays or TNtuples instead? Received on Mon Jul 09 2007 - 18:30:54 CEST

This archive was generated by hypermail 2.2.0 : Mon Jul 09 2007 - 23:50:02 CEST