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 2 of 3.
Abhishek said:
1 decade ago
It is a pointer to a pointer, means it keeps the address of a pointer variable.
Ex: int *q, **p;
Then p = &q;
Ex: int *q, **p;
Then p = &q;
Siddu said:
1 decade ago
**p means?
CHINNA DURAI.S said:
1 decade ago
Why we use C program in mostly?
Sai ram said:
2 decades ago
Any allocation functions like alloc () , malloc () and calloc () should release their memory space with free () function only.
Anju said:
1 decade ago
Can you please explain what is the meaning of
p = (int **) malloc(MAXROW * sizeof(int*));
p = (int **) malloc(MAXROW * sizeof(int*));
Vahitha said:
1 decade ago
What is the difference between alloc(), malloc(), calloc(), please give the clear idea?
Kirthi said:
1 decade ago
dealloc() functions can free the memory space right?
Sham said:
1 decade ago
Please explain about calloc( ) and malloc( )?
Manojkumar said:
1 decade ago
Yeh any allocation functions like alloc() , malloc() and calloc() should release their memory space with free() function only.
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;
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers