How to free the memory?

For example if i have the piece of code as follows:

   CountryName = \(char \*\)malloc\(\(strlen\(CountryName\)\+1\)*sizeof\(char\)\);
    memset\(CountryName, 0, strlen\(CountryName\)\+1\);
    CountryName = SOME VALUE

Now how do i free the memory after use of this code???? :confused:

Use the 'free' command to free memory assigned by the malloc command.

if you use the man command e.g.

man malloc
man free

it should list associated comands under a section called 'see also'. Its always worth reading to the end of the man pages, especially for the behaviour of functions when they encounter error conditions.

and don't forget to test the return value of the malloc function.

void	*my_malloc(unsigned int size)
{
  void	*p;

  p = malloc(size);
  if (p == 0)
    {
      fprintf(stderr, "Error with the malloc function\n");
      exit(1);
    }
  return (p);
}

You can't, since you didn't save a copy of the pointer that you lost when you assigned SOME VALUE to CountryName.

One other point, in line 1, CountryName, I assume has some memory associated with it (since you've used it in the strlen()). The malloc is going to over write that pointer as well, so you'll have an additional storage leak there.