Q:

Freeing node in a linked list

I'm doing some exercise with linked lists in C. And have encountered a pointer problem. I'm trying to implement the function RemoveListByIndex(list, index).
And so far I this code.
void RemoveNodeByIndex(struct node** head,int index) // NOT WORKING YET
{
  // Remove node list
  struct node* current = GetNodeByIndex(*(&head), index);
  struct node* prev    = GetNodeByIndex(*(&head), index-1);

  // For debugging
  PrintHRLine();
  printf("Deleting item %d from list.\n", index);
  printf("Item %d data = %d\n", index, current->data);
  PrintHRLine();
  // Change link from prev link node to the next
  prev->link = current->link;

  // Unlink node wished to delete
  current->link = NULL;
  current->data = 0;

  // Free data
  free((struct node*) current);
}

GetNodeByIndex function:
struct node* GetNodeByIndex(struct node** list, int index)
{
  // Return node by an index given
  // if error it returns NULL
  // for i < index
  //       next_node <- node.link
  //     node <- next.node
  // return next_node

  struct node* current = *list;
  int counter = 0;

  if (current)
    {
      while (current->link != NULL)
    {
      if (counter == index)
        return current;

      current = current->link;
      counter++;
    }
    }
  else
    {
      return NULL;
    }

  return NULL;
}

my list is structured like this:
struct node
{
  int data;
  struct node* link;
};

And I call the function from my main code like this:
int main()
{
  struct node* head = NULL;
  struct node* edit_node = NULL;

  int i;
  edit_node = (struct node*)malloc(sizeof(struct node));

  // Create a list of 10 elements
  for (i = 0; i < SIZE_OF_LIST; i++)
    {
      struct node* item = (struct node*) malloc(sizeof(struct node));
      item->data = i;
      if (i==0)
        head = item;
      printf("(item+%d)->data = %d\n",i,i);
      if (i<SIZE_OF_LIST-1)
    {
      item->link = (item+i+1);
      printf("(item+%d->link = (item+%d+1);\n", i, i);
      printf("(item+%d adress = 0x%lx\n",i, (unsigned long int) &item+i);
    }
      else 
    {
      item->link = NULL;
      printf("(item+%d->link = NULL;\n", i);
    }
}

  // RemoveNodeByIndex(head, 5);
  // PrintListData(&head);
  edit_node->data = 1001;
  AddLastNode(&head, &edit_node);
  SearchNode(&head, 101);
  RemoveNodeByIndex(&head, 8);
  PrintListData(&head);

  // Free memory
  free(item);
  free(edit_node);
  free(head);

  return 0;
}

Everything seems to do fine, except that when I get to the free() call. it fails.
output:
===================================
Deleting item 8 from list. Located : 0x7fffd20ecad0
Item 8 data = 6
===================================
*** Error in `./a.out': double free or corruption (out): 0x00000000011b2090 ***
Aborted (core dumped)

I have a suspicion that I'm handling my pointer some way wrong. 
But what am I doing wrong?
EDIT:
I have included all my source code in the following link : SOURCE CODE
And I've included all the output generated by program here : OUTPUT
Thanks, on advance.

A:

you did not post the complete code, but i think the issue is with free().
as the man page for free() says

free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or  realloc().   Other-
         wise, or if free(ptr) has already been called before, undefined behaviour occurs.  If ptr is NULL, no operation is performed.

I think in your cse, the pointer to current is not allocated by malloc() or calloc(). by doing (*(head)+index); you're pointing to somewhere else.

EDIT:
in your code, instead of using item = (struct node*)malloc(sizeof(struct node) * 10);, try to allocate each node seperately. Also, while deleting nth node, the approach should be go to the n-1th node, get the next [or link] pointer and free() it.

EDIT 2:
How come 
  struct node* current = GetNodeByIndex(*(&head), index);
  struct node* prev    = GetNodeByIndex(*(&head), index);

current and prev are same? What do you think?

