What's the upper-bound for an array index/subscript in GNU bc?

I’ve found that there is an upper-bound on your subscripts/indices in an array in GNU bc. Running interactively and asking to let arr[100000000]=42 returns an error:

 Runtime error (func=(main), adr=17): Array arr subscript out of bounds.

This array size limit isn’t listed among bc’s limits, and it doesn’t appear the "variable names" limit of 32767 affects this since bc accepts arr[100000]=42 without complaint. What is the exact bound on bc’s array subscripts/indices? Is there a way to change this bound?

Asked By: Mike Pierce


Through trial-and-error, the upper-bound is 2²⁴ = 16,777,216. Still not sure why this is the upper-bound or how to change it.

Answered By: Mike Pierce

You can see the bc limits:

$ echo 'limits' | bc
BC_BASE_MAX     = 2147483647
BC_DIM_MAX      = 16777215
BC_SCALE_MAX    = 2147483647
BC_STRING_MAX   = 2147483647
MAX Exponent    = 9223372036854775807
Number of vars  = 32767

And into man bc (1p) we see:

Arrays are singly dimensioned and can contain up to {BC_DIM_MAX}
elements. Indexing shall begin at zero so an array is indexed from 0
to {BC_DIM_MAX}−1.

Looking into 1.07.1 it is defined into file const.h.

/* Definitions for arrays. */

#define BC_DIM_MAX   16777215     /* this should be NODE_SIZE^NODE_DEPTH-1 */

#define   NODE_SIZE        64     /* Must be a power of 2. */
#define   NODE_MASK      0x3f     /* Must be NODE_SIZE-1. */
#define   NODE_SHIFT        6     /* Number of 1 bits in NODE_MASK. */
#define   NODE_DEPTH        4
Answered By: thanasisp
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.