Freeing strings, values, dims etc.

Just starting out? Need help? Post your questions and find answers here.
CadeX
Enthusiast
Enthusiast
Posts: 124
Joined: Mon Oct 02, 2006 2:56 pm
Location: Australia
Contact:

Freeing strings, values, dims etc.

Post by CadeX »

For reinitialization purposes i'm trying to make sure i can completely empty the memory of the program.

Basically, for example:

Code: Select all

String.s="Hello!"
Dim Hi(2)
Hi(1)=3
Debug String
Debug Hi(1)
;Free "String" Memory
;Free "Hi"
Debug String ;Should be 0
Debug Hi(1) ;Should get an error saying the memory doesn't exist.
I've tried

Code: Select all

FreeMemory(@String)
which obviously hasn't worked.

Could someone help fill in the blanks? Thanks for reading.

P.S - Sorry if my post makes no sense, i try my best to explain what i'm after.
Pro-Gamer, Programmer, Pro-Grammer
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

you cannot destroy simple variables despite by a newstart...
arrays you can free by using

Code: Select all

Dim a(0)
oh... and have a nice day.
CadeX
Enthusiast
Enthusiast
Posts: 124
Joined: Mon Oct 02, 2006 2:56 pm
Location: Australia
Contact:

Post by CadeX »

So, doing:

Code: Select all

String.s = "Hi"
and then

Code: Select all

String = ""
Would somewhat free it?
Pro-Gamer, Programmer, Pro-Grammer
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

maybe it FREEs the mem used for the string, but it would not free the pointer....

remember that string-vars in PB are pointers to MEM where the string data risides.
so if you declare

Code: Select all

String.s = "" 
I presume that "String" will hold a pointer to a one-byte-mem that holds an ending 0.
oh... and have a nice day.
CadeX
Enthusiast
Enthusiast
Posts: 124
Joined: Mon Oct 02, 2006 2:56 pm
Location: Australia
Contact:

Post by CadeX »

That's no good then, i need to free the pointer AND the value.
Pro-Gamer, Programmer, Pro-Grammer
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

as far I know there is no native possibility in PB to free simple variables...

is there in C?

anyhow.. what you need it for?
oh... and have a nice day.
CadeX
Enthusiast
Enthusiast
Posts: 124
Joined: Mon Oct 02, 2006 2:56 pm
Location: Australia
Contact:

Post by CadeX »

I'm writing an engine to the best of my ability. It struck me when i came into the posibility of re-initialising the engine without having to restart the entire process. I then came to the point where i may not need the program to use the engine after a certain point... That's when i thought it would be helpful to clear the memory. Although each value uses little to no memory, i can live with it if there's no, "simple", way.
Pro-Gamer, Programmer, Pro-Grammer
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

yeah...
if you want to do something that way, just rely on allocating memory "manually" and asigning it to your own pointers...

surely this is additional efford...

perhaps it may help if you carry all your values not in simple variables
but use an array you can easily free and some constants to address them...
oh... and have a nice day.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

CadeX, may be this is useful:
http://www.purebasic.fr/english/viewtopic.php?t=23099
look at the solution posted by Freak
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

CadeX wrote:I'm writing an engine to the best of my ability. It struck me when i came into the posibility of re-initialising the engine without having to restart the entire process. I then came to the point where i may not need the program to use the engine after a certain point... That's when i thought it would be helpful to clear the memory. Although each value uses little to no memory, i can live with it if there's no, "simple", way.
Then you definetely don't want to free the pointer itself, you want to reset it to its initial value.
Post Reply