char * pointers

Oct 31, 2011 at 2:01pm
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
Oct 31, 2011 at 2:24pm
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
Oct 31, 2011 at 2:30pm
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 Oct 31, 2011 at 2:30pm
Nov 1, 2011 at 11:37am
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
Nov 1, 2011 at 12:06pm
does delete have to be called only when new is present?
Nov 1, 2011 at 12:48pm
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 4:35pm
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??
Nov 1, 2011 at 8:16pm
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.