Advertisement

Why does this give an error?

Started by February 15, 2002 10:44 AM
6 comments, last by Draxis 22 years, 10 months ago
  
	case DESTROY_ALL_DRONES:
		{
			for(int index=0;index<MAX_DRONES;index++) 
			{
				if(list[index]) 
				{
					delete list[index];
					printf("Deleted At: %d\n", index);
				}
			}
		}
  
This gives me an ugly debug error. Something about pHead->nBlockUse Er, what''s wrong with it? I''m just saying that if the array contains something, clear it.
The problem is quite clearly dependent on some other part of your code. You possibly need to reduce your program down to something small and compilable which recreates the error, and post that.

In particular, it would be useful to know the following... What is list? The name suggests it''s a linked list, but I''m not so sure. What elements are held in list? Since you''re attempting to delete them, it needs to hold pointers to objects allocated via new, but it''s not obvious that''s the case.

--
Very simple ideas lie within the reach only of complex minds.
Advertisement
Well, let''s see here.
List is an array which holds objects of structure drones.
Not quite a linked list, but here it is:
  typedef struct drones{	int x;	int y;}drone, *ptr_drone;  

The objects are created and indexed into the array here:
  	case NEW_DRONE:		{			if(drone_num<MAX_DRONES) 			{				list[drone_num] = new drone;				list[drone_num]->x = a;				list[drone_num]->y = b;			}			return list[drone_num];		}  

And here''s the problem:
  	case DESTROY_ALL_DRONES:		{			for(int index=0;index<MAX_DRONES;index++) 			{				if(list[index]) 				{					delete list[index];					printf("Deleted At: %d\n", index);				}			}			return 0;		}  

Consequently, THIS works for some weird reason:
  	case DESTROY_DRONE:		{			if(list[drone_num])			{				delete list[drone_num];			}			return 0;		}	}  

Consequently, all this code doesn''t really do anything, I''m just messing around with some ideas.
the problem is....

a) when creating the array you did not initialize the entire array to "NULL". and therefore one or more of the array elements contain an invalid pointer reference.

b) you deleted the "offending" object and are trying to delete it again.

c) "index" is out of the array bounds and thus would be referencing an invalid pointer reference.


note: "if(list[index])" seems so informal to me. don''t people like this better "if (list[index] != NULL)"? i dunno, it''s just me, i guess! what if "NULL" is not defined as "0" in the future?

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
You''re not clearing the list array to NULL on create.
You''re not setting the deleted entry to NULL on delete.

So, when you do the if(list[index]), it always evaluates to true, and then tries to delete memory that isn''t allocated.
Oh dur, you were right, index was out of array bounds.
When I created list I set it to three elements, instead of MAX_DRONES
Advertisement
Err.. that wasn''t a compilable sample, so there are still missing details. Also, what is the error? You say it gives a debug error, but that could mean one of many things. I''m guessing you''re getting a debug assert, possibly because you''re deleting out of range, but I can''t tell for sure. I''m fairly suspicious that the index value might be going out of range, so you might like to double check that.

--
Very simple ideas lie within the reach only of complex minds.
Okie dokie, I''ve got my code that does absolutely nothing all fixed!
Arrays are initialized to NULL and the indexing is in bounds.
Thanks everyone

This topic is closed to new replies.

Advertisement