How to free memory allocated, I tried free(ptr), but it doesn't work as I accepted it do .
what are alternatives if I'm wrong? why is this behavior in my code?
typedef struct a
{
int a;
int b;
} aa;
aa* ptr = NULL;
int main()
{
//code
int input = 2;
ptr = malloc(sizeof(aa)*input);
for (int i = 0; i < input; i++)
{
ptr[i].a = 10;
ptr[i].b = 20;
}
for (int i = 0; i < input; i++)
{
printf("%d %d\n", ptr[i].a, ptr[i].b);
}
free(ptr);
for (int i = 0; i < input; i++)
{
printf("%d %d\n", ptr[i].a, ptr[i].b);
}
return 0;
}
Output:
10 20
10 20
After free
0 0
10 20
what I'm accepting
Output:
10 20
10 20
After free
0 0
0 0