How can I determine the exact size of the selection buffer that I need to
pass to the function glSelectBuffer?

I’ve seen in many demos that people just pick an arbitrary number like 64,
256, or 512. But this is prone to error, so I do not want to do it this way.

I thought at first the size related to the number of names I assign with LoadNames,
so if I want to select 5 objects, I would create a selection buffer that is
5*sizeof(UINT). But this is not correct as the number of hits returned is
always -1, indicating an overflow.

So, how do I determine its correct size based on the geometry that I want to

The reason why I am asking is that I have geometry that is changing
(num_vertices, num_faces, etc…), so I would like to choose an
appropriate selection buffer size every time it changes.

Any thoughts?

Is this possible? Nothing in the Red book, or any other OpenGL book I know of
explains how this works in detail.

All I can do right now is guess.

It depends on your application. I for example use 64K ints buffer - quite big, but the user sometimes wants to select many objects ( 500-600 control points or triangles) at once. For normal scenes I think you need not more than 10K. So it is your decision. It also depends if you use hierachical or just simple selection.
Hope this helps
(By the way - the memory today is cheap, and I think 64K system memory is nothing compared to the vertices and textures caches and so on… )

So, regardless of your scene, you use a fixed-size buffer?

Does using a huge buffer like 64k affect performance? Does OpenGL ever walk through
the entire buffer memory (to clear it for example) or does it only access the buffer
to write values to it? I would think the latter.