This is from the ENVIRON.C program in Petzold's win32 book.
Code:
void FillListBox (HWND hwndList)
{
int iLength ;
TCHAR * pVarBlock, * pVarBeg, * pVarEnd, * pVarName ;
pVarBlock = GetEnvironmentStrings () ; // Get pointer to environment block
while (*pVarBlock)
{
if (*pVarBlock != '=') // Skip variable names beginning with `=`
{
pVarBeg = pVarBlock ; // Beginning of variable name
while (*pVarBlock++ != '=') ; // Scan until `=`
pVarEnd = pVarBlock - 1 ; // Points to `=` sign
iLength = pVarEnd - pVarBeg ; // Length of variable name
// Allocate memory for the variable name and terminating
// zero. Copy the variable name and append a zero.
pVarName = (TCHAR *) calloc (iLength + 1, sizeof (TCHAR)) ;
CopyMemory (pVarName, pVarBeg, iLength * sizeof (TCHAR)) ;
pVarName[iLength] = '\0' ;
// Put the variable name in the list box and free memory.
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pVarName) ;
free (pVarName) ;
}
while (*pVarBlock++ != '\0') ; // Scan until terminating zero
}
FreeEnvironmentStrings (pVarBlock) ;
}
When does pVarBlock become =0 and therefore false in the first while loop? It seems to me that this code will cause pVarBlock to keep increasing (>0) until it points to the end of the computers memory. The program shouldn't ever exit the while loop.
Since the program works I am obviously missing something and it's probably pretty basic. Can someone explain?