how to free memory in c
int *example = NULL; //create pointer
example = malloc(sizeof(int)); //allocate memory
free(example); //deallocate memory
how to free memory in c
int *example = NULL; //create pointer
example = malloc(sizeof(int)); //allocate memory
free(example); //deallocate memory
n no of array in c using malloc
#include <stdio.h>
int main()
{
printf("nnttStudytonight - Best place to learnnnn");
int n, i, *ptr, sum = 0;
printf("nnEnter number of elements: ");
scanf("%d", &n);
// dynamic memory allocation using malloc()
ptr = (int *) malloc(n*sizeof(int));
if(ptr == NULL) // if empty array
{
printf("nnError! Memory not allocatedn");
return 0; // end of program
}
printf("nnEnter elements of array: nn");
for(i = 0; i < n; i++)
{
// storing elements at contiguous memory locations
scanf("%d", ptr+i);
sum = sum + *(ptr + i);
}
// printing the array elements using pointer to the location
printf("nnThe elements of the array are: ");
for(i = 0; i < n; i++)
{
printf("%d ",ptr[i]); // ptr[i] is same as *(ptr + i)
}
/*
freeing memory of ptr allocated by malloc
using the free() method
*/
free(ptr);
printf("nntttCoding is Fun !nnn");
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us