Page 1 of 1

ReAllocateMemory() Id# problem

Posted: Tue Mar 19, 2024 12:45 pm
by charvista
Hello!
I need to resize 'on the fly' a memory portion, but the problem is that the ID is not the same. How can I do so that it remains THE SAME ID ?

Code: Select all

*f=AllocateMemory(1000)
*g=AllocateMemory(1200)
Debug "*f="+Str(*f)

*f=ReAllocateMemory(*f,MemorySize(*g))
Debug "*f="+Str(*f)
In this example, *f has changed after memory resizing. How can I resize keeping the same ID ?

Re: ReAllocateMemory() Id# problem

Posted: Tue Mar 19, 2024 1:23 pm
by NicTheQuick
It's simple: You can't.

Re: ReAllocateMemory() Id# problem

Posted: Tue Mar 19, 2024 1:26 pm
by NicTheQuick
And a bit more detail: You don't get an identifier, you get a pointer from these commands. And a pointer directly points to a memory region. If you enlarge that memory it might not have enough space behind it to be enlarged in place. Instead your operating system just allocates a completely fresh memory region for you, copies the data to its new place and gives you the new pointer.

[Solved] ReAllocateMemory() Id# problem

Posted: Tue Mar 19, 2024 5:41 pm
by charvista
Thanks Nic(for)TheQuick answer.
I see that it is indeed impossible, and now I understand why.
The only way to keep the same pointer number is to allocate it large enough the first time the memory is allocated.
But that's not possible in my program so I can live with the pointer number change. This is no longer a problem.
Thank you!