C Programming - Memory Allocation - Discussion

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;
}

[A]. memfree(int p);
[B]. dealloc(p);
[C]. malloc(p, 0);
[D]. free(p);

Answer: Option D

Explanation:

No answer description available for this question.

Sai Ram said: (Sep 16, 2010)  
Any allocation functions like alloc () , malloc () and calloc () should release their memory space with free () function only.

Seema said: (Apr 5, 2011)  
It is a obvious answere because memory is freed by free() function. Which is option D.

Muruganandam said: (Jul 18, 2011)  
free() function is used for to free the memory space.

Ashok said: (Jul 30, 2011)  
Yes I agree with seema & ram by free() memory is from allocation.

Renuka said: (Aug 10, 2012)  
When we can use dealloc().

Murugan said: (Oct 22, 2012)  
Can you write a simple program of memory allocation?

Vishal Dalawai said: (Oct 26, 2012)  
@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;

}

Manojkumar said: (Feb 21, 2013)  
Yeh any allocation functions like alloc() , malloc() and calloc() should release their memory space with free() function only.

Sham said: (Jul 10, 2013)  
Please explain about calloc( ) and malloc( )?

Kirthi said: (Nov 26, 2013)  
dealloc() functions can free the memory space right?

Vahitha said: (Dec 10, 2013)  
What is the difference between alloc(), malloc(), calloc(), please give the clear idea?

Anju said: (Jul 11, 2014)  
Can you please explain what is the meaning of

p = (int **) malloc(MAXROW * sizeof(int*));

Jhansi said: (Jul 23, 2014)  
In that program I want to meaning of p=(int**)malloc(MAXROW*sizeof(int*)).

Chinna Durai.S said: (May 7, 2015)  
Why we use C program in mostly?

Siddu said: (Jul 6, 2015)  
**p means?

Abhishek said: (Aug 11, 2015)  
It is a pointer to a pointer, means it keeps the address of a pointer variable.

Ex: int *q, **p;

Then p = &q;

Amit said: (Jan 16, 2016)  
Please explain me how malloc works?

Abhi said: (Jul 8, 2016)  
What is the advantage of malloc and calloc?

Darshan Kg said: (Aug 24, 2016)  
Malloc allocates a single block of memory but calloc allocates multiple blocks of memory. For calloc block, initial value will be zero.

Emanuel said: (Sep 28, 2016)  
I don't understand the casting (int**). The malloc function gives an address of memory where you can put an integer value, i.e., it is a pointer to an integer. How it can help a cutting that it points to a pointer to an integer. It, indeed, only a pointer to an integer?

Ahmed said: (Oct 23, 2016)  
Same problem here, why the type casting (int**)?

p = (int **) malloc(MAXROW * sizeof(int*));

Ahmed said: (Oct 24, 2016)  
I knew the answer, the type casting in malloc because malloc returns a void pointer. So we should type cast it to the type we want to use first!

Sha Vat said: (Oct 12, 2017)  
Here, free() function is used to clear the memory that is dynamically created using calloc(), malloc() functions.

Akash said: (Nov 16, 2017)  
How many bytes allocated ?

Roddur said: (Aug 25, 2018)  
why are we doing malloc(.... sizeof(int*))?

It won't only (int ) do in place of (int *)?

Please anyone explain me.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.