Hi all!

So our T.A. gave us a function to use in our programming assignment as follows:

struct node *delete_from_list(struct node *list, int examnum)
{
	struct node *cur, *prev;
	for (cur = list, prev = NULL; 
		 cur != NULL && cur->examNumber != examnum; 
		 prev = cur, cur =cur ->next)
		 ;
	if(cur == NULL)
	{
		
		return list;		//n was not found
	}
	if (prev == NULL) 
	
		list = list ->next;	
							//n was in the first node
	else 
		prev->next = cur->next;	//n was in another node
	free(cur);
	return list;
}

After about the third time this function is called, I get an error at the free(cur) second line from the bottom saying Windows has inserted a breakpoint possibly due to heap corruption and a few other technical sounding phrases, but I can't make anything out of it. Any idea what's wrong? Is this enough context?

Edited 6 Years Ago by spursfan2110: n/a

Actually, to be more specific, it appears to happen immediately before the free(cur) would be executed...which seems even odder to me.

Actually, to be more specific, it appears to happen immediately before the free(cur) would be executed...which seems even odder to me.

Likely you are writing to already freed memory, outside this delete_from_list() function. The debug memory management facilities kick in upon the attempt to free() , catching the aforementioned error (-> runtime error message).

A minimal example that you could try out to see how it happens, would be along the lines of..

int main()
{
  /*  Allocate a node and initialize its examNumber to 1 */
  struct node * p1 = alloc_node(1);

  /*  Allocate a node and initialize its examNumber to 2 */
  struct node * p2 = alloc_node(2);

  /*  Make a list .. */
  struct node * list = p1;
  p1->next = p2;
  p2->next = NULL;

  /*  free p1 .. */
  list = delete_from_list(list, 1);

  /*  p1 still is accessible here, pointing to the freed memory, 
   *  now write to that memory ..
   */
  p1->next = NULL;

  /*  Try to free p2 .. */
  list = delete_from_list(list, 2);

  return 0;
}

For little more information on how this error gets caught, you might read Magic debug values.

[EDIT]
Also check that you are not trying to free anything twice/nor writing out-of-bounds.

Edited 6 Years Ago by mitrmkar: EDIT

Hi all!

So our T.A. gave us a function to use in our programming assignment as follows:

struct node *delete_from_list(struct node *list, int examnum)
{
	struct node *cur, *prev;
	for (cur = list, prev = NULL; 
		 cur != NULL && cur->examNumber != examnum; 
		 prev = cur, cur =cur ->next)
		 ;
	if(cur == NULL)
	{
		
		return list;		//n was not found
	}
	if (prev == NULL) 
	
		list = list ->next;	
							//n was in the first node
	else 
		prev->next = cur->next;	//n was in another node
	free(cur);
	return list;
}

After about the third time this function is called, I get an error at the free(cur) second line from the bottom saying Windows has inserted a breakpoint possibly due to heap corruption and a few other technical sounding phrases, but I can't make anything out of it. Any idea what's wrong? Is this enough context?

Aha! I figured it out. The problem was, I thought I was mallocing nodes within a loop, but it turns out I only malloced one node prior to the loop, and so I was trying to free nodes that hadn't been malloced. I am not quite sure why it worked for the first few times as opposed to just 1 iteration, but I guess that's one of life's mysteries.

This question has already been answered. Start a new discussion instead.