0

From the 21st century C book:

Static variables, even those inside of a function, are initialized when the program starts, before main, so you can’t initialize them with a nonconstant value.

//this fails: can't call gsl_vector_alloc() before main() starts
static gsl_vector *scratch = gsl_vector_alloc(20);

Why can't gsl_vector_alloc be called before main starts?

Samer
  • 1,923
  • 3
  • 34
  • 54
qed
  • 22,298
  • 21
  • 125
  • 196

1 Answers1

1

What you quoted from the book is the answer, i.e. because it wouldn't be compliant to the C standard.

All the expressions in an initializer for an object that has static storage 
duration shall be constant expressions or string literals.

Although I believe something like this is possible in C++ under certain conditions.

downhillFromHere
  • 1,967
  • 11
  • 11