Is it always necessary to match malloc() and free() calls? I have to allocate dynamic memory for structure and then free it after some operations. Can I overwrite the data in dynamic memory or I should free them first and malloc again? For examples:
int x =5,len = 100;
do{
struct test *test_p = (struct test *) malloc(sizeof(struct test) + len);
/* ---do some operation ---------- */
free(test_p);
x--;
}while(x);
Another approach is to do malloc before loop and do free() inside loop. Can i use this struct pointer after freeing it ? For Example:
int x =5, len = 100;
struct test *test_p = (struct test *) malloc(sizeof(struct test) + len);
do{
/* ---do some operation ---------- */
free(test_p);
x--;
}while(x);
Thanks in advance for your help and suggestions.