ReAllocateMemory() Id# problem

Just starting out? Need help? Post your questions and find answers here.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

ReAllocateMemory() Id# problem

Post 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 ?
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ReAllocateMemory() Id# problem

Post by NicTheQuick »

It's simple: You can't.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ReAllocateMemory() Id# problem

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

[Solved] ReAllocateMemory() Id# problem

Post 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!
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply