2
static const int LOG_MAX = 31;
static int log_table[LOG_MAX];

This code is inside of a function in C. When I try to compile I get the error:

"main.c:19:16: error: storage size of 'log_table' isn't constant".

I don't understand this since LOG_MAX is const.

Just to clarify this is C code and I am using GCC.

6
  • Use a #define instead maybe? Commented Jan 10, 2014 at 13:46
  • 4
    Which programming language are you asking about? C and C++ are different languages and in this case you will get different answers, depending on which programming language standard version that is used. Commented Jan 10, 2014 at 13:47
  • the problem is the declaration of static const, i answered bellow Commented Jan 10, 2014 at 13:55
  • @JoãoPinho Unfortunately, your answer was incorrect... Commented Jan 10, 2014 at 13:57
  • @Lundin why? it compiles fine Commented Jan 10, 2014 at 13:58

5 Answers 5

4

In older C and C++ standards, the array bounds of an array had to be a constant literal evaluated at compile time. A const variable isn't necessary evaluated at compile time, it could be created in runtime as a local variable. Also, as pointed out in another answer, const should actually be regarded read-only rather than anything else.

In all C and C++ standards, static arrays must always have their size set using a constant literal. (Or to be picky, this applies to any variable with static storage duration)

In newer C standards (C99, C11) however, the code you posted is perfectly fine if you leave out the static keyword. It will then create a variable-length array (VLA), which may or may not be what you wanted to do.

I'm not sure about the latest C++11 standard, but as far as I know it does not support VLAs.

Sign up to request clarification or add additional context in comments.

Comments

3

const does not mean constant in C but rather read-only. LOG_MAX is not a constant in your program.

Here are two ways to have a constant:

#define LOG_MAX 31

or

enum {
    LOG_MAX = 31
};

Comments

1

This isn't valid C, even though the int is const and static. I would recommend doing something like

#define LOG_MAX 31
static int log_table[LOG_MAX];

3 Comments

@svk variable length arrays with static storage duration are not supported in c99.
@svk Not for static arrays, it's not valid even in C99.
Oh, my mistake, didn't notice static.
1

This fails since a const int variable is not considered a compile-time constant in C.

But since the array is going to be static, you can be sure that the "full" declaration is always available, i.e. it's never going to be referenced through a pointer. Thus, you can inline the size and use sizeof in all places where you wanted to use LOG_MAX:

static int log_table[32];

and then elsewhere in the same function:

const size_t log_max = sizeof log_table / sizeof *log_table;

This keeps the "magic constant" around, but only in one place and its purpose should be pretty clear given the way the log_table is used. This is all inside a single function, after all, it's not scary global data.

Comments

0

You should use the preprocessor :

#define LOG_MAX 31
static int log_table[LOG_MAX];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.