Showing posts with label Loops. Show all posts
Showing posts with label Loops. Show all posts

Thursday, 20 October 2011

How can you tell whether a loop ended prematurely?

Generally, loops are dependent on one or more variables. Your program can check those variables outside the loop to ensure that the loop executed properly. For instance, consider the following example:

#define REQUESTED_BLOCKS 512
int x;
char* cp[REQUESTED_BLOCKS];


/* Attempt (in vain, I must add...) to
allocate 512 10KB blocks in memory. */


for (x=0; x< REQUESTED_BLOCKS; x++)
{
cp[x] = (char*) malloc(10000, 1);