char * pointers

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
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
closed account (DGvMDjzh)
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
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
does delete have to be called only when new is present?
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
does delete have to be called only when new is present?

As you worked out, delete should only be called if the object was allocated with new.

You were passing the address of a local variable??
yh too much stress does reduce reasoning, but no I was just feeding it an array like this "message" as an argument
Topic archived. No new replies allowed.