Friday, July 31, 2009

C code question?

#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;





int main ()


{


int i, n;


int *pointerData;





printf ("Enter number of items to be stored: ");


scanf ("%d", %26amp;i);


pointerData = (int*) calloc (i, sizeof(int));





if (pointerData==NULL)


exit (1);





for (n = 0; n %26lt; i; n++)


{


printf ("Enter number #%d: ", n);


scanf ("%d", %26amp;pointerData[ n ]);


}


printf ("You have entered: ");





for (n = 0; n %26lt; i; n++)


printf ("%d ", pointerData[ n ]);


free (pointerData);


system("pause");


return 0;


}





can somebody tell me when does the first if statement will occur?


I know that when *pointerData is NULL. But what would the user enter so that pointerData will become NULL.





thanks

C code question?
You might also do like this so you can get a visual clue as to what's going on:





if (pointerData==NULL)


{


printf("Memory allocation failed! Exiting program...\n");


exit (1);


}





Otherwise, I'd say this code is fairly well written. Another poster had an issue with your cast of calloc's (void *) of allocated memory to an (int *). IMO, all this does is silence a compiler warning, and shouldn't affect performance whatsoever. ANSI experts and purists will disagree--and probably for good reason--because someday, in some instance, it could bite you, yes. But in many years of programming experience, I haven't run into that instance yet. Opinion mode, "off". And good luck.





Oh! And to answer your question, "when does the first if statement will occur?" ... almost immediately after you enter a value at the prompt:





printf ("Enter number of items to be stored: ");


scanf ("%d", %26amp;i);





The code execution progresses top down from main(), and there are no loops or anything to significantly delay execution to the first if(), (except the wait for the user to enter a value). A microsecond after the user enters a value, I'd say, is when the first if() gets hit...





I see one more question, "But what would the user enter so that pointerData will become NULL"





Answer: any number that is too large for calloc() to be able to allocate memory for that many integers.





sizeof(int) is variable depending on implementation, but generally 2 to 4 bytes. Now, we're allocating n * sizeof(int), so number of bytes of memory requested could be 2 to 4 times greater than n...that's important.





But certainly, and generally, any value of n less than 1,000 should give no problem...unless you have a hundred other applications running, or something equally bizarre.





////////////////////EDIT//////////


Thumbs up for you, CatNip! Poster wasn't asking when if() executes, but when it's true.





Now, see? I should have been able to translate that question.... :-) Kudos...
Reply:if there isn't enough memory for the calloc() to allocate the amount of space requested, then it will return a null pointer.
Reply:pointerData is set by the calloc() call. If calloc fails, because, for instance, the computer is out of free memory, then pointerData will be NULL.





You should ALWAYS ALWAYS ALWAYS test the results of calloc or malloc for success.





Also, calloc and malloc's result should NOT be cast like this.
Reply:The 1st if statement will be true if one of the following happens:





1. The calloc fails. See the errno for the error value.


2. If either input parameter into calloc is 0, then it may return a Null or it may not. It depends on the implementation, in which case the 1st If statement may not be true.

hyacinth

No comments:

Post a Comment