So the pointer thing, does freebasic do
something auto-magically regarding declaring
a pointer or after use?
Should it be:
Dim aptr As Integer Ptr = NULL
or does this suffice?
Dim aptr As Integer Ptr
Also it seems that, pending on the structure
of the code, setting the pointer to NULL after
use is preferable:
aptr = NULL
None of the documentation, as far as I can see,
mentions this:
https://www.freebasic.net/wiki/TutPterDataType
https://www.freebasic.net/wiki/TutPointers
It seems to be a common practice in C:
https://www.geeksforgeeks.org/null-pointer-in-c/
and then there is some contention as well:
https://stackoverflow.com/questions/122 ... itializing
set ptr to null and or declare ptr as null (pointer)
Re: set ptr to null and or declare ptr as null (pointer)
There aren't any 'best practices' for FreeBasic, so some of the C ones regarding pointers apply to FB's pointers as well. Other than that, exercise common sense: setting a pointer to null or not 'after use' depends on what will happen with that pointer next, for example, if that pointer is owned by some object (you set it to null to indicate 'not owning any pointer right now'), or if it goes out of scope (no need to set it to null, as it won't be accessible anymore).
A null pointer might also be used to indicate other things, such as 'not found' (when searching in a list of objects, say), that a function failed (for example, to load an image), or to indicate 'uninitialized pointer'. It depends on the specific use case.
A null pointer might also be used to indicate other things, such as 'not found' (when searching in a list of objects, say), that a function failed (for example, to load an image), or to indicate 'uninitialized pointer'. It depends on the specific use case.
Last edited by paul doe on May 26, 2025 12:19, edited 1 time in total.
Re: set ptr to null and or declare ptr as null (pointer)
Declaring a pointer as:
'Dim aptr As Integer Ptr'
(without an initializer) always initializes its value to 0.
The keywords 'Deallocate' and 'Delete' do not reset the pointer to 0 (its value is not changed).
To avoid the dangling pointer effect, the user can explicitly reset its value to 0 after freeing the associated memory.
'Dim aptr As Integer Ptr'
(without an initializer) always initializes its value to 0.
The keywords 'Deallocate' and 'Delete' do not reset the pointer to 0 (its value is not changed).
To avoid the dangling pointer effect, the user can explicitly reset its value to 0 after freeing the associated memory.
Re: set ptr to null and or declare ptr as null (pointer)
First of many thanks for the speedy reply much appreciated.
@Paul
could cause crashes or unpredictable behavior' ?
@fxm
@fxm
So pro forma:
@Paul
This addresses the 'dangling pointer' issue, which, in turn,exercise common sense: setting a pointer to null or not 'after use' depends on
what will happen with that pointer next for example, if that pointer is owned
by some object (you set it to null to indicate 'not owning any pointer right now')
could cause crashes or unpredictable behavior' ?
@fxm
Ah, in this case, 0 is the freebasic equivalent of NULL?Declaring a pointer as:
'Dim aptr As Integer Ptr'
(without an initializer) always initializes its value to 0.
@fxm
After freeing the associated memory?To avoid the dangling pointer effect, the user can explicitly reset its value to 0 after freeing the associated memory.
So pro forma:
Code: Select all
Dim aptr As Integer Ptr 'is initialized to 0 by fb unless explicitly specified other wise
'do something with aptr
Deallocate aptr ' free memory ?
aptr = 0 'null or 0 are interchangeable?
Re: set ptr to null and or declare ptr as null (pointer)
NULL is not a pure FreeBASIC keyword, but you can define it by:
'#define NULL 0'
Example of memory allocation then deallocation:
'#define NULL 0'
Example of memory allocation then deallocation:
Code: Select all
Dim As Zstring Ptr pz '' declare a Zstring pointer with its value set to 0
pz = Callocate(10) '' allocate a memory of 10 bytes and clear the content (set to 0)
*pz = "FreeBASIC" '' fill the memory with the ascii codes of "FreeBASIC" + terminal null character
Print "'" & *pz & "'" '' print the Zstring from allocated memory
Deallocate pz '' free the allocated memory
Print "'" & *pz & "'" '' print the Zstring from freed memory (so with a dangling pointer!)
pz = 0 '' reset the pointer
Print "'" & *pz & "'" '' print the Zstring with the reseted pointer
Sleep
Re: set ptr to null and or declare ptr as null (pointer)
It's not a C or C++ keyword either. It's just a standard #define.
Re: set ptr to null and or declare ptr as null (pointer)
Only the 'ReAllocate' keyword usage allows to automatically return a null pointer in case of deallocation.
Syntax:
result = ReAllocate( pointer, count )
- If pointer is null (0), then ReAllocate behaves identically to Allocate.
- ElseIf pointer is valid and count is null (0), then ReAllocate behaves similar to Deallocate and a null (0) pointer is returned.
- Else the previous allocated memory is reallocated with 'count' bytes, and 'result' is the address of the reallocated memory.
('pointer' value is never modified)
Usage example for allocate and deallocate:
On the other hand, '[C]Allocate'/'DeAllocate' can be mixed with 'ReAllocate'.
Syntax:
result = ReAllocate( pointer, count )
- If pointer is null (0), then ReAllocate behaves identically to Allocate.
- ElseIf pointer is valid and count is null (0), then ReAllocate behaves similar to Deallocate and a null (0) pointer is returned.
- Else the previous allocated memory is reallocated with 'count' bytes, and 'result' is the address of the reallocated memory.
('pointer' value is never modified)
Usage example for allocate and deallocate:
Code: Select all
Dim As Zstring Ptr pz '' declare a Zstring pointer with its value set to 0
pz = Reallocate(0, 10) '' allocate a memory of 10 bytes (the content is not set to 0)
Print pz '' print the pointer value
*pz = "FreeBASIC" '' fill the memory with the ascii codes of "FreeBASIC" + terminal null character
Print "'" & *pz & "'" '' print the Zstring from allocated memory
pz = Reallocate(pz, 0) '' reallocate the memory to 0 bytes <=> deallocate the memory
Print pz '' print the pointer value
Print "'" & *pz & "'" '' print the Zstring with the reseted pointer
Sleep
On the other hand, '[C]Allocate'/'DeAllocate' can be mixed with 'ReAllocate'.
Re: set ptr to null and or declare ptr as null (pointer)
Once again to all many thanks for the input.
Just a small twist on this pointer thing
all that has been discussed applies to:
https://www.freebasic.net/wiki/CatPgMemory
or freebasic's Memory Functions, specifically
'Dynamic Memory' calls with
regard of deallocation right?
So when it come to external libs like
sdl, sqlite, bass, etc they often have
there own memory call for example sdl has:
where using:
is a no no.
seem to have there own internal
memory management as well see:
https://sqlite.org/c3ref/close.html
https://sqlite.org/c3ref/free.html
However clearing the pointer
is still considered a good practice
of course heeding Pauls advice:
'It depends on the specific use case'
right?
Just a small twist on this pointer thing
all that has been discussed applies to:
https://www.freebasic.net/wiki/CatPgMemory
or freebasic's Memory Functions, specifically
'Dynamic Memory' calls with
regard of deallocation right?
So when it come to external libs like
sdl, sqlite, bass, etc they often have
there own memory call for example sdl has:
Code: Select all
SDL_DestroyTexture(texture)
SDL_DestroyRenderer(renderer)
SDL_DestroyWindow(glass)
SDL_GL_DeleteContext(glContext)
SDL_free(map)
SDL_GameControllerClose(controller)
Code: Select all
Deallocate texture
or:
Deallocate renderer
Code: Select all
sqlite3_close(db)
sqlite3_free(errMsg)
memory management as well see:
https://sqlite.org/c3ref/close.html
https://sqlite.org/c3ref/free.html
However clearing the pointer
Code: Select all
texture = 0
renderer = 0
db = 0
errMsg = 0
of course heeding Pauls advice:
'It depends on the specific use case'
right?