R
rohit
Hi,
Iam confused as to when is the memory freed in this program.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
union test{
char *i;
char *ch;
};
int main()
{
union test test;
test.ch = malloc(6);
strcpy(test.ch,"hello");
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.i);
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.ch);
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
return 0;
}
And the output I get from my solaris box is :
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
regards
rohitash
Iam confused as to when is the memory freed in this program.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
union test{
char *i;
char *ch;
};
int main()
{
union test test;
test.ch = malloc(6);
strcpy(test.ch,"hello");
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.i);
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.ch);
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
return 0;
}
And the output I get from my solaris box is :
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
regards
rohitash