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.
Why does this give an error?
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.
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.
Well, let''s see here.
List is an array which holds objects of structure drones.
Not quite a linked list, but here it is:
The objects are created and indexed into the array here:
And here''s the problem:
Consequently, THIS works for some weird reason:
Consequently, all this code doesn''t really do anything, I''m just messing around with some ideas.
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.
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.
February 15, 2002 11:11 AM
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.
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
When I created list I set it to three elements, instead of MAX_DRONES
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.
--
Very simple ideas lie within the reach only of complex minds.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement