I'm working with some code that uses the environment array _environ. It contains a few sections which look like this:-

        char** the_environ = _environ;

        for (size_t i = 0; the_environ[i]; ++i) {
                string estring = the_environ[i];
                // Do some other stuff
What's happening is that the loop is stepping off the end of the array. At first I thought the solution was obvious. It seemed to me that ++i should really be i++. Unfortunately, even after changing that, I'm still having problems. Let's say there are 16 elements in the environment array. It seems to me that element the_environ[16] isn't guaranteed to be NULL. For example if there were originally 17 entries and then I delete one of them, the_environ[16] seems to get left as a bad pointer.

Is there a better way to get the number of available environment strings?