-->
Showing posts with label to understand free() function. Show all posts
Showing posts with label to understand free() function. Show all posts

to understand free() function

//understanding free()
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *x ;//pointer declaration
    x= (int*)malloc(10*sizeof(int));//allocates the memory for 10 integers(10x4=40)
                                    // to heap

    if(x==NULL)//tests whether the memory is available
    {
        printf("error in allocating");//displays error if no memory
    }
    else
    {
    printf("%d\n", sizeof(x));//prints value 4. Because the
                            //sizeof thinks what memory each value/integer will occupy
    }
    free(x);//frees the memory.
    return 0;
}