malloc: error checking and freeing memory
Posted
by yCalleecharan
on Stack Overflow
See other posts from Stack Overflow
or by yCalleecharan
Published on 2010-04-04T14:12:26Z
Indexed on
2010/04/04
14:23 UTC
Read the original article
Hit count: 754
Hi, I'm using malloc to make an error check of whether memory can be allocated or not for the particular array z1. ARRAY_SIZE is a predefined with a numerical value. I use casting as I've read it's safe to do so.
long double *z1 = (long double *)malloc(sizeof (long double) * ARRAY_SIZE);
if(z1 == NULL){
printf("Out of memory\n");
exit(-1);
}
The above is just a snippet of my code, but when I add the error checking part (contained in the if statement above), I get a lot of compile time errors with visual studio 2008. It is this error checking part that's generating all the errors. What am I doing wrong?
On a related issue with malloc, I understand that the memory needs to be deallocated/freed after the variable/array z1 has been used. For the array z1, I use:
free(z1);
z1 = NULL;
Is the second line z1 = NULL necessary?
Thanks a lot...
© Stack Overflow or respective owner