Poke Modifying Previous Address' Value

General FreeBASIC programming questions.
dodicat
Posts: 7083
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Poke Modifying Previous Address' Value

Postby dodicat » Jul 23, 2021 10:55

I never use peek or poke.
Is there a deallocate bug in general?
A memory slot can be shared and used after deallocation, ( just nit picking).

Code: Select all



Type myData
    Dim As Integer i
    Dim As Short s
    Dim As Byte b
End Type


dim as myData ptr z=allocate(sizeof(mydata))
print "Adress of z  ";z
deallocate z



Dim As myData Ptr pd = New myData
print "Adress of pd "; pd

poke mydata,pd,type<mydata>(4,5,6)

print
print peek (mydata,z).i '<----- should be pd, not z deallocated
print peek (mydata,z).s '            "
print peek (mydata,z).b '            "



poke mydata,z,type<mydata>(40,50,60)

print peek (mydata,pd).i '<---- vice versa
print peek (mydata,pd).s           
print peek (mydata,pd).b

delete pd

Sleep


Code: Select all

Adress of z  1710032
Adress of pd 1710032

 4
 5
 6
 40
 50
 60
 
SARG
Posts: 1280
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Poke Modifying Previous Address' Value

Postby SARG » Jul 23, 2021 12:24

No bug.

'Deallocate' says to the sytem this memory is free but don't set the pointer as null.

'New' asks the system a piece of memory, in your case by chance the previous one is given. So the 2 pointers point to the same memory space.

To avoid potential problems some recommend to set the pointer to null after deallocation.
dodicat
Posts: 7083
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Poke Modifying Previous Address' Value

Postby dodicat » Jul 23, 2021 13:43

Thanks SARG, it is just a fluky use of memory.
But peek and poke I have never used in any code, although, with care it works perfectly.
fxm
Moderator
Posts: 10531
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Poke Modifying Previous Address' Value

Postby fxm » Jul 24, 2021 16:01

'Peek' returns a reference (with FreeBASIC), so we can also use it to write to a memory address and not use 'Poke' at all (this was not the case with QuickBASIC):

Code: Select all

Type myData
    Dim As Integer i
    Dim As Short s
    Dim As Byte b
End Type

Dim As myData Ptr pd = New myData

peek (mydata,pd) = type(4,5,6)

print peek (mydata,pd).i
print peek (mydata,pd).s           
print peek (mydata,pd).b

delete pd
Sleep
Vortex
Posts: 63
Joined: Sep 19, 2005 9:50

Re: Poke Modifying Previous Address' Value

Postby Vortex » Jul 25, 2021 9:48

Just for fun, a quick example to emulate LCase with Poke and Peek :

Code: Select all

Dim As Zstring Ptr s,t

Dim As Byte a

Dim As Integer i

s=@"THis IS a TEst."
t=s

For i=1 To Len(*s)

   a=Peek(s)
   Poke s,a Or -(a>65 And a<97) Shl 5
   s=s+1

Next i

Print *t
fxm
Moderator
Posts: 10531
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Poke Modifying Previous Address' Value

Postby fxm » Jul 25, 2021 13:03

Vortex wrote:Just for fun, ...

Your code does not work with gcc because gcc uses a '.rdata' (read only) section to store the zstring literal, and does not allow the program to write to it (gas or gas64 only uses a single 'data' section).
Vortex
Posts: 63
Joined: Sep 19, 2005 9:50

Re: Poke Modifying Previous Address' Value

Postby Vortex » Jul 26, 2021 7:09

Hi fxm,

I analyzed the executable with LordPE and the tool reports two sections rdata anda data. The chracteristics of the data section is C0300040 which means readable\executable section containing initialized data. The string THis IS a TEst. is located in the data section ( not rdata ) so no any problem. The sample code is built with 32 bit FreeBASIC v1.08.1

EDIT : It's the 64-bit version of FreeBASIC compiler causing the problem, I will check it.
Vortex
Posts: 63
Joined: Sep 19, 2005 9:50

Re: Poke Modifying Previous Address' Value

Postby Vortex » Jul 26, 2021 8:05

Hi fxm,

Thanks for the heads-up. Could you try the new version below? A macro is defined to move the ZStrings to the data section.

Code: Select all

#macro Zstr(var,string)

Dim var As Zstring Ptr

Asm

.data

var##z:

.asciz string

.text

   push OFFSET var##z

#ifdef __FB_64BIT__

   #define DATA_TYPE_DEF QWORD

#else

   #define DATA_TYPE_DEF DWORD

#endif

   pop DATA_TYPE_DEF PTR [var]

End Asm

#endmacro


Dim As Zstring Ptr t

Dim As Byte a

Dim As Integer i

Zstr(s,"THis IS a TEst.")

t=s

For i=1 To Len(*s)

   a=Peek(s)
   Poke s,a Or -(a>65 And a<97)*32
   s=s+1

Next i

Print *t
fxm
Moderator
Posts: 10531
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Poke Modifying Previous Address' Value

Postby fxm » Jul 26, 2021 8:28

Yes, OK now.

To be more precise on your previous version:
- this worked for 32-bit [and '-gen gas']
- this worked for 64-bit and '-gen gas64'
- this did not work for 32-bit and '-gen gcc'
- this did not work for 64-bit [and '-gen gcc']
dodicat
Posts: 7083
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Poke Modifying Previous Address' Value

Postby dodicat » Jul 26, 2021 9:15

I use a simpler, easier to follow reusable macro (for me anyway) perhaps, just for fun.

Code: Select all

#macro Zstr(var,string)
#ifndef var
dim as zstring * 500 temp = string
dim as zstring ptr var=strptr(temp)
#else
temp=string
var=strptr(temp)
#endif
#endmacro



Dim As Zstring Ptr t

Dim As Byte a

Dim As Integer i

Zstr(s,"THis IS a TEst.")

t=s

For i=1 To Len(*s)

   a=Peek(s)
   Poke s,a Or -(a>65 And a<97)*32
   s=s+1

Next i

Print *t



Zstr(s,"THis IS another BiGgEr TEst.")
t=s

For i=1 To Len(*s)

   a=Peek(s)
   Poke s,a Or -(a>65 And a<97)*32
   s=s+1

Next i

Print *t
sleep

 

Return to “General”

Who is online

Users browsing this forum: No registered users and 11 guests