Skip to content
Latchkey

gcc/clang "array bound is not an integer constant" in CI

A C++ fixed-size array needs a compile-time constant bound. A non-const variable, a function result, or a runtime value cannot size it, and C++ has no variable-length arrays.

What this error means

Declaring T arr[n] where n is a non-const variable fails with "array bound is not an integer constant", though it may compile as a GNU extension elsewhere.

gcc
app.cpp:5:11: error: array bound is not an integer constant before ']' token
    5 |   int data[n];
      |           ^

Common causes

How to fix it

Use a constant bound or std::vector

  1. Make the bound constexpr if it is known at compile time.
  2. For runtime sizes, use std::vector instead of a fixed array.
gcc
#include <vector>
std::vector<int> data(n);   // runtime-sized
constexpr int N = 16; int fixed[N];   // compile-time

How to prevent it

  • Size fixed arrays with constexpr values and reach for std::vector whenever the length is only known at runtime.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →