C / GL: using -1 as a counter in an unsigned array of integers

I pass in an array of vertex indices in some GL code ... each element is GLushort

I want to finish using a sentinel device to avoid the painstaking passage of the length of the array each time along with 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 complete, as some of the elements have a value of 0

Is it possible to use -1?

As I understand it, this will lead to the maximum integer that GLushort can represent, which would be ideal.

But is this behavior guaranteed in C?

(I cannot find the equivalent constant MAX_INT for this type, otherwise I would use this)

+3
source share
4 answers

GLushort , (GLushort)-1 GLushort. C , . , -1.

, C89 SIZE_MAX size_t. #define SIZE_MAX ((size_t)-1).

, , (GLushort)-1 , .

+5

:

const GLushort GLushort_SENTINEL = (GLushort)(-1);

, , 2 .

, C, ( ). : , C.....

+1

GLushort - UNSIGNED_SHORT, unsigned short , C , OpenGL 2 ^ 16-1 ( 4.3 ), ( , unsigned short ).

, -1, , , , , if(), " ", , , , , , . , , .

, 0xffff, jv42, , .

+1

If you need a named constant, you should not use a variable constas suggested in another answer. They really are not the same. Use a macro (as others have pointed out) or an enum type constant:

enum { GLushort_SENTINEL = -1; };

The standard ensures that it is always int(a really different constant name -1) and that it will always translate to the maximum value of your unsigned type.

Edit: or you could have it

enum { GLushort_SENTINEL = (GLushort)-1; };

if you fear that some architectures GLushortmay already be unsigned int.

0
source

All Articles