space() & garbage collection

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

space() & garbage collection

Post by jassing »

For garbage collection, do strings get cleaned up as soon as the procedure is over? I have some older routines that I used string variables and space() vs pointers & allocatememory()

when the result is less than the allocated space, is that 'extra' space cleaned up when it's out of scope or immediately?

procedure.s something()
v$ = space(1024)
newlen = someapi(@v$,1024) ; let's say v$ now holds "hello", when is that allocated space discarded?
procedurereturn v$
endproocedure
Taz
User
User
Posts: 75
Joined: Sat Jan 20, 2018 5:28 pm
Location: Germany

Re: space() & garbage collection

Post by Taz »

Try it out, then you'll be sure. :D
Example to test: viewtopic.php?t=70152
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: space() & garbage collection

Post by AZJIO »

jassing
You are copying the string to pass it to the call point. In the procedure, it will be destroyed immediately. For example, try passing not a string, but a pointer to a string, and you will see that there is nothing there.
Last edited by AZJIO on Mon May 01, 2023 8:06 pm, edited 1 time in total.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: space() & garbage collection

Post by jassing »

Taz wrote: Mon May 01, 2023 6:43 pm Try it out, then you'll be sure.
Interesting read.. seemed there was an issue, but no more -- I'll poke around. In my case I am doing hundreds of calls per minute and want to be sure memory isn't an issue - if it is; I'll cover to allocating & freeing the memory myself... but it sounds like Fred addressed it.

@AZJIO - Спасибо

Cheers
-j
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: space() & garbage collection

Post by AZJIO »

If the string will not be increased then you can use code like this
i.e. the string is always within #MAX_PATH (260 characters)
i.e. there will be no memory re-allocation

Code: Select all

Procedure NewPath(*str, n)
	Protected *p.string = @*str
	GetModuleFileName_(0, *str, #MAX_PATH)
	PathRemoveFileSpec_(*str)
	*p\s + "\file.dll"
EndProcedure

Define Path$ = Space(#MAX_PATH)
NewPath(@Path$, #MAX_PATH)
Debug Path$
If the string is going to be incremented, i.e. as a regular text string, then you need to use the struct

Code: Select all

Procedure Test(*p.String)
	*p\s + "\file.dll"
EndProcedure

Define Text.String
Text\s = "system"
Test(@Text)
Debug Text\s
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: space() & garbage collection

Post by jassing »

I think for my sanity, I'll go thru and use allocatememory()/freememory() instead of space()
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: space() & garbage collection

Post by AZJIO »

Using Space() allows you to work with a variable both as a string and as a pointer to data. If you are using AllocateMemory(), then you will have to use PeekS() and PokeS(), meaning you will be copying the memory to the variable multiple times. The task that is being solved above is to transfer data without copying string variables or to reduce these operations to a minimum. In fact, Space() does this by filling the memory with spaces 1 time. Now count how many times you will have PeekS() and PokeS() executed. From this you decide which option is more suitable.

By the way, AllocateMemory() also fills the data with zeros. And if this is not done, then there is a chance to read more than it is allocated. The Space() function also has a terminating Null at the end, which also makes it safe to read beyond the line.

With PeekS() you create a string variable. In this case, AllocateMemory() is doing you a disservice.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: space() & garbage collection

Post by jassing »

@AZJIO, thank you for that - that was helpful in getting a better understanding. spasiba.
In my head this resulted in less remaining space being allocated, which is why I thought 'i'm controlling the memory, this is good"
*p = allocatememory(4096)
l =someapi( *P, 4096)
str$ = peeks(*p,l)
freememory(*p)
since the string would almost certainly be less than the allocated space, where as if i did
str$ = space(4096)
someapi(@str$,4096)
would result in 4096 being more permanently allocated, and if that was done many times a minute by various threads it would add up.
but then, if it's not even peeks()'d it seems it would be better to use allocatememory(),no?
Maybe I need to revisit everywhere I use allocatememory too... food for thought, for sure.
SMaag
Enthusiast
Enthusiast
Posts: 325
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: space() & garbage collection

Post by SMaag »

procedure.s something()
v$ = space(1024)
newlen = someapi(@v$,1024) ; let's say v$ now holds "hello", when is that allocated space discarded?
procedurereturn v$
endproocedure
v$ is created on Stack! What needs more time is to fill it with Spaces
when leaving the procedure, the stackpointer will be restored automatically.
There will be no memory problem!

But if someapi is a Windows-API, the API use 1BYTE-ASCII-Strings, what needs a conversion between 1ByteChar to 2ByteChar
Post Reply