为什么在主启动之前不能调用gsl_vector_alloc?

时间:2014-10-17 19:26:49

标签: c static gsl

从21世纪的C书:

  

初始化静态变量,即使是函数内部的变量   当程序启动时,在main之前,所以你无法初始化它们   具有非常数值。

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

为什么在主要启动之前不能调用gsl_vector_alloc?

1 个答案:

答案 0 :(得分:1)

你从书中引用的是答案,即因为它不符合C标准。

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

虽然我相信在某些条件下C ++可能会出现类似的情况。

相关问题