Non-static-const variable in array dimension

I wrote a function that behaves like this

Float_t function(Int_t n) 
    Int_t size = n - 5;
    Float_t array[size];

and when I run it from the root prompt with .x it generates the error:

Error: Non-static-const variable in array dimension FILE:test.C LINE:9
(cint allows this only in interactive command and special form macro which
is special extension. It is not allowed in source code. Please ignore
subsequent errors.)

so I read that and changed the code to something like

Float_t function(Int_t n) 
    static const Int_t size = n - 5;
    Float_t array[size];

and now I get a segmentation violation, which I attatched. I know the variable isn’t necessary, in my case it’s just to make the code more readable. I am just wondering what the problem is. Also, it must have something to do with the rest of my code, because it doesn’t happen in a small test program. I attatched the program as well.

It is not possible to test your problem without your adta file.
I suggest
-removing the line gROOT->Reset() at the beginning of your function. This statement should never be used inside a named script.
See Users Guide.

-compiling with ACLIC
instead of
root > .x AnalyzeCfirradData.cxx
root > .x AnalyzeCfirradData.cxx+

To run with ACLIC, you have to
-move AnalyzeCfirradData as the last function in your file because it references other functions. (or declare the prototypes).

-add some includes:
#include <TFile.h>
#include <TTree.h>
#include <Riostream.h>


Using both your suggestions fixed the problem, thanks. However, I find some more strange behavior. I modified the source to include prototypes and remove the gROOT->Reset(). Everything works as expected when I use ACLIC, but without it the “static const” variables are not set as expected. For instance, the arithmetic (n passed into function)

cout << "n is " << n << endl;
 static const Int_t start = 1;    // ignore low point
 static const Int_t end = n - 2;  // ignore steep high points
 static const Int_t size = end - start;

 printf("n %u start %i end %i size %i",n,start,end,size);
 cout << endl;

‘.x AnalyzeCfirradData.cxx’ prints out:
n is 13
n 13 start 1 end -2 size -3

while ‘.x AnalyzeCfirradData.cxx+’ prints:
n is 13
n 13 start 1 end 11 size 10

… so when using CINT and the keywords “static const”, variables in the assignment are set to zero? I could not reproduce this with a small test file, so the difference must be somewhere else in my program.
test.cxx (326 Bytes)
CfirradTree.root (18.9 KB)
AnalyzeCfirradData.cxx (4.8 KB)

I just got the problem to reproduce in the small test script by switching from addition to subtraction. Run the new test script with and without ACLIC to see.

Hi Ricky,

What you are trying to achieve is not legal in the current C++ standard.

The size of array on the stack has to be fixed at compile time (or at least at the first run). A const static variable get its value set only ONCE. Hence you array array will at best be set to the first value your use to call the function.

Instead of using a array of the stack, you should use either an STL container (like a vector), a ROOT collection (TArrayF), or a
C-style array on the heap: float*f=new float[t];.....;delete [] f;.


As already stated by Philippe, non of following 4 functions are valid
C++ code. But, I am surprized to see that g++ accepts them all.
This will confuse users.

Float_t f1(Int_t n) {
Int_t size = n - 5;
Float_t array[size];

Float_t f2(Int_t n) {
const Int_t size = n - 5;
Float_t array[size];

Float_t f3(Int_t n) {
static const Int_t size = n - 5;
Float_t array[size];

Float_t f4(Int_t n) {
static Int_t size = n - 5;
Float_t array[size];