Search code examples
carraysopengl-es-2.0unsigned-integersentinel

C/GL: Using -1 as sentinel on array of unsigned integers


I am passing an array of vertex indices in some GL code... each element is a GLushort

I want to terminate with a sentinel so as to avoid having to laboriously pass the array length each time alongside the array itself.

#define SENTINEL ( (GLushort) -1 ) // edit thanks to answers below
:
GLushort verts = {0, 0, 2, 1, 0, 0, SENTINEL};

I cannot use 0 to terminate as some of the elements have value 0

Can I use -1?

To my understanding this would wrap to the maximum integer GLushort can represent, which would be ideal.

But is this behaviour guaranteed in C?

(I cannot find a MAX_INT equivalent constant for this type, otherwise I would be using that)


Solution

  • If GLushort is indeed an unsigned type, then (GLushort)-1 is the maximum value for GLushort. The C standard guarantees that. So, you can safely use -1.

    For example, C89 didn't have SIZE_MAX macro for the maximum value for size_t. It could be portably defined by the user as #define SIZE_MAX ((size_t)-1).

    Whether this works as a sentinel value in your code depends on whether (GLushort)-1 is a valid, non-sentinel value in your code.