WriteData and Invalid memory access

Just starting out? Need help? Post your questions and find answers here.
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: WriteData and Invalid memory access

Post by Caronte3D »

Time to a bug report or maybe a feature request :?
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: WriteData and Invalid memory access

Post by Simo_na »

ok... BYTE...maybe i did some mistakes, but all of them is made by me ?

Code: Select all

testVar=9
Debug PeekB(@testvar)  ;9
Debug PeekA(@testvar)  ;9
Debug "----"
testVar=128
Debug PeekB(@testvar) ;-128
Debug PeekA(@testvar) ;128
Debug "----"
testVar=255
Debug PeekB(@testvar) ;-1
Debug PeekA(@testvar) ;255
Debug "----"
testVar=256
Debug PeekB(@testvar) ;0
Debug PeekA(@testvar) ;0
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: WriteData and Invalid memory access

Post by infratec »

Your code and the results are Ok.
B is signed A is unsigned.

Use

Code: Select all

testVar=9
ShowMemoryViewer(@testVar, SizeOf(Integer))
Debug PeekB(@testvar)  ;9
Debug PeekA(@testvar)  ;9
Debug "----"
testVar=128
ShowMemoryViewer(@testVar, SizeOf(Integer))
Debug PeekB(@testvar) ;-128
Debug PeekA(@testvar) ;128
Debug "----"
testVar=255
ShowMemoryViewer(@testVar, SizeOf(Integer))
Debug PeekB(@testvar) ;-1
Debug PeekA(@testvar) ;255
Debug "----"
testVar=256
ShowMemoryViewer(@testVar, SizeOf(Integer))
Debug PeekB(@testvar) ;0
Debug PeekA(@testvar) ;0
And debug it in singlestep, then you see why the last result is 0
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: WriteData and Invalid memory access

Post by Simo_na »

infratec wrote: Wed Feb 16, 2022 11:14 pm ShowMemoryViewer(@testVar, SizeOf(Integer))
8)

Thank you
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: WriteData and Invalid memory access

Post by Simo_na »

netmaestro wrote: Tue Feb 15, 2022 9:38 pm Probably a limitation of WriteData(). This works:

Code: Select all

Global gigs.q = 4*1024*1024*1024   ; 4 Gigabyte

*MemoryID = AllocateMemory(gigs)        
If *MemoryID > 0
  FillMemory(*MemoryID,gigs,255,#PB_Byte)  
  If CreateFile(0,GetTemporaryDirectory()+"\Text.txt")                
    FileBuffersSize(0,#MAXWORD)                                        
    *writeptr=*MemoryID
    *end = *MemoryID + gigs
    While *writeptr < *end
      If *end-*writeptr >= #MAXWORD
        chunksize = #MAXWORD
      Else 
        chunksize = *end-*writeptr
      EndIf
      WriteData(0, *writeptr, chunksize)            
      *writeptr + chunksize
    Wend
    FlushFileBuffers(0)                     
    CloseFile(0)                             
  EndIf
EndIf
Hello
could this version also be valid for only 4 Gigs ?

Code: Select all

Global.q gigs = (4*1000)*1024*1024   
*MemoryID = AllocateMemory(gigs)       
FillMemory(*MemoryID,gigs,255,#PB_Byte)
CreateFile(0,"Text.txt")                 
WriteData(0, *MemoryID,(gigs/2))
WriteData(0, *MemoryID+gigs/2,gigs/2)        
CloseFile(0)

All the best.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: WriteData and Invalid memory access

Post by Olli »

Hello !

A short remark : if you want to be cross-platform (equ to be used everywhere), limit your memory blocks as below

512 MebiBytes or
(1 << 29) bytes

Code: Select all

*maxMem = AllocateMemory(1 << 29)
If you don't like #29, reduce it :

Code: Select all

*maxMem = AllocateMemory(1 << 28)     ; 256 MB
And if you want to keep performancy, by preventing your OS from dividing immediatly your memory block, and from transmiting it to harddrive, reduce it again :

Code: Select all

*maxMem = AllocateMemory(1 << 24)     ; 16 MB
With 16 MB (24-bits access), you will be insured to use quickly the memory whatever the size, and whatever the computer.

To address the whole memory you want to allocate, use an memory indices table, as below :

Code: Select all

Structure miTable
 baseMem.I[0]
EndStructure
or

Code: Select all

Structure miTable
   Array *baseMem(0)
EndStructure
If you need to use a hard searching/scaning/moving, you can allocate a 256 MB memory block, and you can store 16 indices (pointors) in your memory indices tables, which points 16*16 MB of contiguous memory block. So, you can execute fast synchronizations, and, normally, the CPU and the OS are able to execute them on a first 256 MB memory block, during a second 256 MB memory block is loading from disk, and during you are modifying 16 other memory blocks of 16 MB. All that, at the same time, without soft thread (the hardware manages it, what it means the CPU can do lots of things during this time).

Code: Select all

; factice example
CopyMemory(*a1st256MBmem + x, *a1st256MBmem + y, CopySize)
ReadData(File, *a2nd256MBmem, LoadSize)
PokeI(*a16MBmem + a, PeekI(*a16MBmem + b) + myAdd)
Probably, these three lines are executed in any periods which have time unions between themselves.


All this above is my humble opinion.

errata I removed all the (1 << x - 1)
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: WriteData and Invalid memory access

Post by Olli »

Sometimes, there is a byte order mark on the top header of the file : I ever check it if it exists.ReadStringFormat()
Post Reply