B
BigChuck19
My textbook asserts that when checking if sufficient memory was allocated
for a
dynamic variable using malloc(), a test should be made to determine
whether
sufficient memory was actually allocated. I understand the concept of
checking
if the memory was allocated; the code confuses me, however. E.g., a
fragment
of code from the book is:
grades = (int *) malloc(numgrades * sizeof(int));
if (grades == (int *) NULL) {
printf("Failed to allocate grades array");
exit(1);
}
My question concerns the use of the cast (int *). The book states that
one
must write " if (grades == (int *) NULL) " because if there is
insufficient
memory available, malloc() returns NULL, which is then cast into a
pointer to
an integer by the (int *) preceding the call to malloc(). I do not
understand
how NULL can be cast into anything, though, since I thought NULL was the
constant 0, or I guess its value is the constant 0. To check if malloc()
returns NULL or not, why couldn't one write " if (grades == NULL) ". I
guess
the answer is that you can't write that because NULL was cast as a
pointer to
an integer in the previous statement. But what does it matter which data
type
NULL points to, as long as you determine that the address stored in
grades is
NULL?
The use of a cast on NULL raises a few more questions. Say I create a
character array called array. After initializing all its entries, I then
want
to clear the entire array by making each entry hold the null character. I
could write something like the following to accomplish this:
for(i = 0; i < MAXTENTRIES; i++)
array = '\0';
Does the following loop perform the identical operation?
for(i = 0; i < MAXENTRIES; i++)
array = (char) NULL;
And I assume if the preceding code is valid, then one can cast NULL as
any data
type; for instance,
for(i = 0; i < MAXENTRIES; i++)
array = (int) NULL;
would assign each entry the value of zero?
I am confused about the definition of NULL and the use of NULL; is it
only used
when using pointers? I am not sure, since the use of a cast on NULL
seems to
indicate that one can make NULL any data type.
My final question concerns the use of NULL with a structure.
If I define a structure, say,
typedef struct house {
int floors;
char owner[MAXSIZE];
} House;
and then declare h to be a variable of type house
House h;
Now does
h = (House) NULL;
have any meaning?? If so, what does the statement do?
Thanks for your help
Chuck
for a
dynamic variable using malloc(), a test should be made to determine
whether
sufficient memory was actually allocated. I understand the concept of
checking
if the memory was allocated; the code confuses me, however. E.g., a
fragment
of code from the book is:
grades = (int *) malloc(numgrades * sizeof(int));
if (grades == (int *) NULL) {
printf("Failed to allocate grades array");
exit(1);
}
My question concerns the use of the cast (int *). The book states that
one
must write " if (grades == (int *) NULL) " because if there is
insufficient
memory available, malloc() returns NULL, which is then cast into a
pointer to
an integer by the (int *) preceding the call to malloc(). I do not
understand
how NULL can be cast into anything, though, since I thought NULL was the
constant 0, or I guess its value is the constant 0. To check if malloc()
returns NULL or not, why couldn't one write " if (grades == NULL) ". I
guess
the answer is that you can't write that because NULL was cast as a
pointer to
an integer in the previous statement. But what does it matter which data
type
NULL points to, as long as you determine that the address stored in
grades is
NULL?
The use of a cast on NULL raises a few more questions. Say I create a
character array called array. After initializing all its entries, I then
want
to clear the entire array by making each entry hold the null character. I
could write something like the following to accomplish this:
for(i = 0; i < MAXTENTRIES; i++)
array = '\0';
Does the following loop perform the identical operation?
for(i = 0; i < MAXENTRIES; i++)
array = (char) NULL;
And I assume if the preceding code is valid, then one can cast NULL as
any data
type; for instance,
for(i = 0; i < MAXENTRIES; i++)
array = (int) NULL;
would assign each entry the value of zero?
I am confused about the definition of NULL and the use of NULL; is it
only used
when using pointers? I am not sure, since the use of a cast on NULL
seems to
indicate that one can make NULL any data type.
My final question concerns the use of NULL with a structure.
If I define a structure, say,
typedef struct house {
int floors;
char owner[MAXSIZE];
} House;
and then declare h to be a variable of type house
House h;
Now does
h = (House) NULL;
have any meaning?? If so, what does the statement do?
Thanks for your help
Chuck