C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - General Questions (Q.No. 3)
3.
How will you free the memory allocated by the following program?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int **p, i, j;
p = (int **) malloc(MAXROW * sizeof(int*));
return 0;
}
Discussion:
25 comments Page 1 of 3.
Sai ram said:
2 decades ago
Any allocation functions like alloc () , malloc () and calloc () should release their memory space with free () function only.
Seema said:
1 decade ago
It is a obvious answere because memory is freed by free() function. Which is option D.
Muruganandam said:
1 decade ago
free() function is used for to free the memory space.
Ashok said:
1 decade ago
Yes I agree with seema & ram by free() memory is from allocation.
Renuka said:
1 decade ago
When we can use dealloc().
Murugan said:
1 decade ago
Can you write a simple program of memory allocation?
Vishal Dalawai said:
1 decade ago
@Murugan.
Below i have wrote code for memory allocation & free for 2-D array(3 Rows, 4 Columns)
#include<stdio.h>
#include<stdlib.h>
#define ROW 3
#define COL 4
int main()
{
int i;
int **arr;
// Memory allocation for Rows..
arr = (int**)malloc(ROW*sizeof(int*));
if(arr == NULL)
{
printf("Memory allocation for Row failed..\n");
exit(1);
}
// Memory allocation for Columns..
for(i=0; i<ROW; i++)
{
arr[i] = (int*)malloc(COL*sizeof(int)); // each row having 4 cols
if(arr[i] == NULL)
{
printf("Memory allocation for Column Elements failed..\n");
exit(1);
}
// Memory Free..
for(i=0; i<ROW; i++)
free(arr[i]);
free(arr);
return 0;
}
Below i have wrote code for memory allocation & free for 2-D array(3 Rows, 4 Columns)
#include<stdio.h>
#include<stdlib.h>
#define ROW 3
#define COL 4
int main()
{
int i;
int **arr;
// Memory allocation for Rows..
arr = (int**)malloc(ROW*sizeof(int*));
if(arr == NULL)
{
printf("Memory allocation for Row failed..\n");
exit(1);
}
// Memory allocation for Columns..
for(i=0; i<ROW; i++)
{
arr[i] = (int*)malloc(COL*sizeof(int)); // each row having 4 cols
if(arr[i] == NULL)
{
printf("Memory allocation for Column Elements failed..\n");
exit(1);
}
// Memory Free..
for(i=0; i<ROW; i++)
free(arr[i]);
free(arr);
return 0;
}
Manojkumar said:
1 decade ago
Yeh any allocation functions like alloc() , malloc() and calloc() should release their memory space with free() function only.
Sham said:
1 decade ago
Please explain about calloc( ) and malloc( )?
Kirthi said:
1 decade ago
dealloc() functions can free the memory space right?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers