Oct 31, 2011 at 2:01pm UTC
hi
How can char * pointers be deleted? I've tried this code but it doesn't work. All it does is return the debug assertion error
//current level name is a char * pointer
1 2
if (current_level_name)
delete [] current_level_name; //clear array
Last edited on Oct 31, 2011 at 2:03pm UTC
Oct 31, 2011 at 2:24pm UTC
The delete statement looks fine.
How is current_level_name being allocated?
Andy
P.S. It's safe to pass NULL to delete, so you don't need the if statement in this case.
Last edited on Oct 31, 2011 at 2:25pm UTC
Oct 31, 2011 at 2:30pm UTC
You should set the pointer to NULL after deleting it in order to clear the data.
1 2 3 4 5 6
char *array = new char [64];
array = "Array: I'M ALIVE!!! \n" ;
if (array) printf(array);
delete [] array;
array = NULL;
if (!array) printf("*Silence*" );
Last edited on Oct 31, 2011 at 2:30pm UTC
Nov 1, 2011 at 11:37am UTC
current level name is being allocated as:
char * current_level_name;
And the way I pass data to it is through coping another pointer in a function
1 2 3 4 5 6 7
void GameLevel:: Initialize(char * cnewname)
{
//this is how I'm passing data to it
current_level_name = cnewname;
....
return ;
}
Last edited on Nov 1, 2011 at 11:40am UTC
Nov 1, 2011 at 12:06pm UTC
does delete have to be called only when new is present?
Nov 1, 2011 at 12:48pm UTC
problem solved: turns out I had a destructor issue, apparently a destructor shouldn't be called manually since C++ handles it when it goes out of scope
Nov 1, 2011 at 8:16pm UTC
yh too much stress does reduce reasoning, but no I was just feeding it an array like this "message"
as an argument