Pointers: How to remove Strings & Entities

Posted By: Davidus

Pointers: How to remove Strings & Entities - 12/30/08 21:10

Hello,

i have a pointer-understanding problem:
I wanted to write a little console,
but if i want to remove a string that should be displayed,
there is something strange:
It doesn't get removed, it simply stays,
and if i test with "if (currentString == NULL)" or
"if (!currentString)", the programm tells me the
string is still there.

I have also a problem with that on other places
of my programm, where i get errors because
i am accessing removed entitis,the problem there
is the same: a check on "Entity==NULL" doesn't success,
it tells me the entity was there and then crashes when
i access them!
(I won't list up all the code because thats the same problem, only in bigger!)

So how exactly do i get rid of a pointer and its content ?
Do i have to use ptr_remove, and additionally
set the variable to null by "variable = NULL" ?
I know it works if i set the variable on Null manually for strings, but this leads to the problem that i'm filling up my memory with Garbage.

Please, someone gimme an explanation here.

(I read the manual about that and i have some programming experience, but not in c-like languages)

Code:
STRING* currentString;
var consoleLifetime;

void print(STRING* str)
{
	consoleLifetime = 100;
	currentString = str;
}

void showConsole()
{
	if (!currentString)
	{
		return;
	}
	consoleLifetime--;
	draw_text(currentString,10,10, vector(255,255,255));
	if (consoleLifetime < 0)
	{
		ptr_remove(currentString);
	}
}


Spoken precisely,
i would need a function that removes an object,
and sets any pointer pointing to it to NULL,
not to an undefined state.
Unfortunately, the following had no effect,
because it only removes the local given pointer inside the function, not the pointers outside the function that i
really wanted to hit with that (like "currentString" in my previous example)
but i need something in the style of this:

Code:
void delete (ENTITY* ent)
{
	ptr_remove (ent);
	ent = NULL;
}

Posted By: Davidus

Re: Pointers: How to remove Strings & Entities - 01/01/09 02:20

C'mon ,

you cannot tell me that nobody here knows how to deal with pointers ^^

if it has a reason that nobody answers me, please be polite/honest and tell me what's wrong with my post, i am willing to learn if my post was in some kind unfitting or too noobish or whatever.
When i finnally have enough knowledge, i will gladly help other noobs out but at the moment i'm a noob by myself laugh
Posted By: MrGuest

Re: Pointers: How to remove Strings & Entities - 01/01/09 10:36

hey,

really you shouldn't be removing pointers unless you're sure you won't be using them again,

you only need to change the contents of the pointer to NULL,
Posted By: Cowabanga

Re: Pointers: How to remove Strings & Entities - 01/01/09 10:43

use "ptr_remove(POINTER_NAME);"
Posted By: Davidus

Re: Pointers: How to remove Strings & Entities - 01/01/09 16:52

okay but what's the alternative ?

For Strings i can indeed simply set it to null,
but if i remove an entity, how do i get removed all pointers pointing to this thing.

Let's say i had an entity, and 3 monsters who have this entity as a target pointer -
if i removed theentity, the pointers of the 3 monsters are not NULL but INVALID instead, and i'm searching for a way to remove things and remove their pointers as well.

I had another idea: setting on of the 8 flags to null, and let the things remove themselves when they are in their loop - is that a good idea, or how do you remove your entitis, that's what i'm looking for !

By the way, Thanks for the replies so far!
Posted By: MrGuest

Re: Pointers: How to remove Strings & Entities - 01/01/09 20:11

use
ent_remove(ENTITY*);

any pointer pointing to an entity that's been removed will then return NULL
Posted By: Davidus

Re: Pointers: How to remove Strings & Entities - 01/01/09 20:56

Hey MrGuest,

that's what i thought, but it's not.
If i have 3 pointers pointing to X,
and i do ent_remove on pointer nr.1 , then all three
pointers are still pointing to it's adress,
and are causing bugs if called ... that exactly is my problem.
© 2023 lite-C Forums