Freeing the allocated block

N

new

Hi all,
I need your suggestions/comments on the code snip below.

<< snip >>
typedef struct rose {
int k;
char c;
}my_red;

boolean fun(my_red **x,int i)
{
my_red *c=NULL;
c= malloc(sizeof(my_red));
if(!c)
return FALSE;
c->k = i;
*x = c;
return TRUE;
}

int main(void)
{
my_red *ptr = NULL;
if( ! fun(&ptr,9))
printf("Error");
else
printf("k= %d",*ptr);
return 0;
}

<< End Snip >>

Now my question is how to free memory block allocated to variable c in
fun() being in main() function.Does freeing the ptr variable frees the
memory block? If so how?

Thanks a lot in advance.
 
J

Jens Thoms Toerring

new said:
Hi all,
I need your suggestions/comments on the code snip below.
<< snip >>
typedef struct rose {
int k;
char c;
}my_red;
boolean fun(my_red **x,int i)
{
my_red *c=NULL;
c= malloc(sizeof(my_red));
if(!c)
return FALSE;
c->k = i;
*x = c;
return TRUE;
}
int main(void)
{
my_red *ptr = NULL;
if( ! fun(&ptr,9))
printf("Error");
else
printf("k= %d",*ptr);
return 0;
}
<< End Snip >>
Now my question is how to free memory block allocated to variable c in
fun() being in main() function.Does freeing the ptr variable frees the
memory block? If so how?

Calling free() on ptr will do the job since ptr points to the
allocated memory block (or is NULL in case of failure of malloc()).
Please note that saying "memory block allocated to variable c" is
a bit unprecise. What actually happens is that in c an address
value is stored - there's no other connection between c and the
memory block. Consider the following case, which is just a sim-
plification of your example code:

int *x, *y;
x = malloc( 100 * sizeof *x );
y = x;
free( y );

This is correct code since both x and y point to the same allo-
cated memory block. So, if the value returned by malloc() is e.g.
0xDEADBEEF then both x and y have the same value, 0xDEADBEEF.
Now calling free() with x or y as the argument is just like
writing

free( 0xDEADBEEF );

i.e. it's immaterial if you call free() with x or y, the only
important thing is the value that gets passed to free().

Regards, Jens
 
B

Barry Schwarz

Hi all,
I need your suggestions/comments on the code snip below.

<< snip >>
typedef struct rose {
int k;
char c;
}my_red;

boolean fun(my_red **x,int i)
{
my_red *c=NULL;
c= malloc(sizeof(my_red));
if(!c)
return FALSE;
c->k = i;
*x = c;
return TRUE;
}

int main(void)
{
my_red *ptr = NULL;
if( ! fun(&ptr,9))
printf("Error");
else
printf("k= %d",*ptr);

*ptr is not an int. It is an object of type struct rose. Passing
this object to printf when the %d requires an int invokes undefined
behavior. If you want to test the contents of the struct that ptr
points to, use ptr->k.
 
N

new

Thanks a lot for your responses.

*ptr is not an int.  It is an object of type struct rose.  Passing
this object to printf when the %d requires an int invokes undefined
behavior.  If you want to test the contents of the struct that ptr
points to, use ptr->k.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top