Re: Memory reallocation within a sub function...
Posted: Mon Apr 07, 2025 9:46 am
Demivec, this is incredibly helpful. Thank you for taking the time to post this.
I think my initial misunderstanding stems from the fact that in PB, "*" is not treated as an indirection operator as it is in C. Because, if it is, then makes no sense as you would be dereferencing the pointer twice.
As pointers are integers representing memory addresses, surely I can just use an integer variable name for a pointer without the prefix "*".
Which is simpler in my mind, as when I want to dereference I will be reminded to use the @ prefix, rather than making the mistake of thinking that the pointer is automatically dereferenced with "*" like it is in C.
The following simple example works fine, so I can see no obvious reason why the "*" name prefix is needed.
But, as you have echoed what the PB help says, i.e. that "Pointers' names include the '*' prefix at all times except within structures", why exactly does one need to specify the "*" prefix in front of a variable name?
Apart from being two completely different integer variables, does PB treat myvar and *myvar differently in some way, or is this just to improve readability of the code?
I think my initial misunderstanding stems from the fact that in PB, "*" is not treated as an indirection operator as it is in C. Because, if it is, then
Code: Select all
@*MemPtr
As pointers are integers representing memory addresses, surely I can just use an integer variable name for a pointer without the prefix "*".
Which is simpler in my mind, as when I want to dereference I will be reminded to use the @ prefix, rather than making the mistake of thinking that the pointer is automatically dereferenced with "*" like it is in C.
The following simple example works fine, so I can see no obvious reason why the "*" name prefix is needed.
Code: Select all
EnableExplicit
Procedure foo(myvar.i)
Debug("myvar=0x"+Hex(myvar,#PB_Quad))
Debug("peeki(myvar)="+Str(PeekI(myvar)))
EndProcedure
Define myvar.i
myvar=AllocateMemory(65536)
PokeI(@myvar,12345)
foo(@myvar)
Apart from being two completely different integer variables, does PB treat myvar and *myvar differently in some way, or is this just to improve readability of the code?