You shouldn't be mixing Allocate/DeAllocate and New/Delete. If you use Allocate, also use deallocate. If you use New. also use Delete.
Allocate allocates memory of the size you request and spits out a pointer. DaAllocate takes a pointer and marks it invalid. It doesn't destroy anything, which is why the manual says to clear a string before dallocating a pointer to a string.
New and Delete require a pointer to a type, so the type can can be determined with "TypeOf" but that is not really required for new/delete.
What exactly are you trying to do? I fear you are getting into a mess.
I am just wondering since when you call Deallocate, you only indicate the start of the block of memory to return to the system. I figured the system would need to know the amount of memory to return so it must be stored somewhere.
Therefore subsequent to the Allocate call, given a memory address that was allocated, can I find out how much was originally allocated?
I seem to remember that I discovered the length is in the qword before the pointer which you get back. Don't think this is publicised anywhere You could test fairly easily whether this is the case.
wallyg wrote: ↑Sep 30, 2023 6:31
Therefore subsequent to the Allocate call, given a memory address that was allocated, can I find out how much was originally allocated?
fbc's allocate/callocate/deallocate/reallocate eventually call malloc/calloc/free/realloc in the C runtime (CRT).
extern "c"
#if defined(__FB_WIN32__)
#ifdef __FB_64BIT__
declare function getBufferSize cdecl alias "_msize" _
( byval p as any ptr ) as ulongint
#else
declare function getBufferSize cdecl alias "_msize" _
( byval p as any ptr ) as ulong
#endif
#elseif defined(__FB_LINUX__)
#ifdef __FB_64BIT__
declare function getBufferSize cdecl alias "malloc_usable_size" _
( byval p as any ptr ) as ulongint
#else
declare function getBufferSize cdecl alias "malloc_usable_size" _
( byval p as any ptr ) as ulong
#endif
#else
#error getBufferSize not implemented
#endif
end extern
'' test
var a = allocate(100)
var b = allocate(200)
var c = allocate(999)
print getBufferSize(a)
print getBufferSize(b)
print getBufferSize(c)
sleep
But, there's no guarantee that the CRT implements this and it is different on every platform.
If you search the forum for "_msize" or "malloc_usable_size" and you can see a couple of other places this was mentioned.