set ptr to null and or declare ptr as null (pointer)

New to FreeBASIC? Post your questions here.
Post Reply
thrive4
Posts: 86
Joined: Jun 25, 2021 15:32

set ptr to null and or declare ptr as null (pointer)

Post by thrive4 »

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
paul doe
Posts: 1880
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: set ptr to null and or declare ptr as null (pointer)

Post by paul doe »

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.
Last edited by paul doe on May 26, 2025 12:19, edited 1 time in total.
fxm
Moderator
Posts: 12577
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: set ptr to null and or declare ptr as null (pointer)

Post by fxm »

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.
thrive4
Posts: 86
Joined: Jun 25, 2021 15:32

Re: set ptr to null and or declare ptr as null (pointer)

Post by thrive4 »

First of many thanks for the speedy reply much appreciated.
@Paul
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')
This addresses the 'dangling pointer' issue, which, in turn,
could cause crashes or unpredictable behavior' ?

@fxm
Declaring a pointer as:
'Dim aptr As Integer Ptr'
(without an initializer) always initializes its value to 0.
Ah, in this case, 0 is the freebasic equivalent of NULL?

@fxm
To avoid the dangling pointer effect, the user can explicitly reset its value to 0 after freeing the associated memory.
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?
fxm
Moderator
Posts: 12577
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: set ptr to null and or declare ptr as null (pointer)

Post by fxm »

NULL is not a pure FreeBASIC keyword, but you can define it by:
'#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
caseih
Posts: 2199
Joined: Feb 26, 2007 5:32

Re: set ptr to null and or declare ptr as null (pointer)

Post by caseih »

It's not a C or C++ keyword either. It's just a standard #define.
fxm
Moderator
Posts: 12577
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: set ptr to null and or declare ptr as null (pointer)

Post by fxm »

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:

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'.
thrive4
Posts: 86
Joined: Jun 25, 2021 15:32

Re: set ptr to null and or declare ptr as null (pointer)

Post by thrive4 »

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:

Code: Select all

SDL_DestroyTexture(texture)
SDL_DestroyRenderer(renderer)
SDL_DestroyWindow(glass)
SDL_GL_DeleteContext(glContext)
SDL_free(map)
SDL_GameControllerClose(controller)
where using:

Code: Select all

Deallocate texture
or:
Deallocate renderer
is a no no.

Code: Select all

sqlite3_close(db)
sqlite3_free(errMsg)
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

Code: Select all

texture   = 0
renderer  = 0
db        = 0
errMsg    = 0
is still considered a good practice
of course heeding Pauls advice:
'It depends on the specific use case'
right?
Post Reply