AllocateMemory, FreeMemory and UseMemory

Just starting out? Need help? Post your questions and find answers here.
Fiwip
User
User
Posts: 27
Joined: Tue Feb 24, 2004 2:42 pm

AllocateMemory, FreeMemory and UseMemory

Post by Fiwip »

Hi,

So far I understand with PB 3.89 things in memory are working as below :

*Pointer=AllocateMemory(BufferSize)
FreeMemory(*Pointer)

but UseMemory() is removed,

What is the way to "replace" this one ?

Can you give me an example ?

Thanks,

Fp
filperj
User
User
Posts: 77
Joined: Tue Sep 16, 2003 8:53 pm
Location: Nevers(France)

Post by filperj »

UseMemory() was only used to retrieve the address of the first byte of the memory bank, if I'm not missing something...
Now this address is already stored in your "*Pointer" variable, and is the only thing you have to handle the bank.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

So is FreeMemory no longer needed? How do I free a block of memory
when done with it, if there's no memory id number anymore?
Fiwip
User
User
Posts: 27
Joined: Tue Feb 24, 2004 2:42 pm

Post by Fiwip »

Filperj: Thanks for the reply

PB: Excepted if I missed something in your question, my above post should answer your question.
*Pointer=AllocateMemory(BufferSize)
FreeMemory(*Pointer)
Fp
Fiwip
User
User
Posts: 27
Joined: Tue Feb 24, 2004 2:42 pm

Post by Fiwip »

Need some help according to this topic.

The code below is from one PB's coder but I can't remember who, sorry.

For my starting in PB I have changed & cancelled some stuff to see how things are working. Anyhow the prog was working perfectly under PB 3.81 after my changing. Test it.

Now under 3.89 beta 2, I changed instructions according rules mentionned above, but the prog crash PB and I don't know why, where & how to handle this.

Can somebody have a look inside.

Thanks,

Fp

Note: the prog ask you to open a file .txt, then count words and write them in a new file .txt.
The instructions moved between 3.81 and 3.89 b2 are specified as " for 3.81 or for 3.89 b2"

Code: Select all

NLines.l
NWords.l
CurrentDirectory.s
EOL.s
Filename.s

Dim AsciiConv.s(255)
Dim AllWords.s(10000000)
Dim UniqueWords.s(1000000)
Dim WordCount.l(1000000)

Global NLines, NWords, EOL, CurrentDirectory, AsciiConv, Allwords, UniqueWords, WordCount, NUniqueWords

Global *FileBuffer ; --------------------------------------------------- for 3.89 b2 

;#FileBufferMem = 0 ----------------------------------------------------- for 3.81
 
Global MemFileOffset.l, MemFileSize.l


Procedure.l LoadFileToMem(fileID,fname.s) 
  
  Protected fileID,fname 
  
  If ReadFile(fileID,fname)
    
    MemFileSize = Lof()
    Debug "MemFileSize = " + Str(MemFileSize)
    ;*FileBuffer = AllocateMemory(#FileBufferMem,MemFileSize,0)---------- for 3.81
    *FileBuffer = AllocateMemory(MemFileSize) ;-------------------------- for 3.89 b2
    
    If *FileBuffer
      ReadData(*FileBuffer,MemFileSize)
    EndIf 
    
    CloseFile(fileID)
    MemFileOffset = 0
  
  EndIf 
  
  ProcedureReturn *FileBuffer
    
EndProcedure


Procedure MoreInMem()
  
  If MemFileOffset < MemFileSize
    ok = 1
  EndIf
  
  ProcedureReturn ok
  
EndProcedure


Procedure.s ReadLineFromMem()
  ;CallDebugger
  ;*FileBuffer = UseMemory(#FileBufferMem) -------------------------------- for 3.81
  Debug *FileBuffer
  If *FileBuffer And MoreInMem()
    Start = *FileBuffer + MemFileOffset
    Debug Start
    Length = 0
    Repeat
      Length + 1
      Debug Length
      Byte.b = PeekB(Start + Length)
      Debug Byte
      Debug MemFileOffset + Length
    Until  Byte = 13 Or Byte = 10 Or MemFileOffset + Length >= MemFileSize
  EndIf
  Skip = 1
  Byte = PeekB(Start + Length + 1)
  Debug Byte
  If Byte = 10 Or Byte = 13
    Length + 1
    Debug Length
    Skip + 1
    Debug Skip
  EndIf
  MemFileOffset + Length
  Debug MemFileOffset + Length
  Debug PeekS(Start + 1, Length - Skip)
  ;DisableDebugger
  ProcedureReturn PeekS(Start + 1, Length - Skip)
EndProcedure


Procedure CloseFileMem()
  ;FreeMemory(#FileBufferMem) --------------------------------------------- for 3.81
  FreeMemory(*FileBuffer) ;------------------------------------------------ for 3.89 b2
EndProcedure


;Procedure SelectTheFile()
 ; CallDebugger
 ; Filename.s = OpenFileRequester("Select a file", CurrentDirectory + "\" + "*.txt", "Text files|*.txt|All files|*.*", 0, #PB_Requester_MultiSelection)
 ; Debug "filename" + Filename
 ; ProcedureReturn Filename
;EndProcedure


Procedure ParseFile(Filename.s)
  
  SetGadgetText(100, "Processing file " + Filename)
  CurrentDirectory = GetPathPart(Filename)
  
  If LoadFileToMem(0, Filename)
    
    While MoreInMem()
      NLines + 1
      a$ = Trim(ReadLineFromMem())
      b$ = ""
      
      For I = 1 To Len(a$)
        b$ = b$ + AsciiConv(Asc(Mid(a$, I, 1)))
      Next
      
      If b$ <> ""
        b$ = ReplaceString(b$, "  ", " ")
      EndIf
      
      b$ = LCase(Trim(b$))
      
      If Len(b$) <> 0
        
        While FindString(b$, " ", 1) <> 0
          AllWords(NWords) = Mid(b$, 1, FindString(b$, " ", 1) - 1)
          NWords + 1
          b$ = Mid(b$, FindString(b$, " ", 1) + 1, Len(b$) - FindString(b$, " ", 1) - 1 + 1)
        Wend
        
        AllWords(NWords) = b$
        NWords + 1
        
      EndIf
      
    Wend
    
    StatusBarText(0, 0, "Parsing line #" + Str(NLines) + " ... found " + Str(NWords) + " words.", 0)
  
  EndIf
  
  NWords - 1

EndProcedure


Procedure CreateUniqueWordList()
  
  j = 0
  UniqueWords(j) = AllWords(j)
  WordCount(j) = 1
  
  For I = 1 To NWords
    
    If AllWords(I) <> AllWords(I - 1)
      j + 1
      UniqueWords(j) = AllWords(I)
      WordCount(j) = 1
    Else
      WordCount(j) + 1
    EndIf
    
  Next
  
  NUniqueWords.l = j

EndProcedure


Procedure CreateWordFile()
  
  If CreateFile(0, "result.txt")
    For z = 0 To NUniqueWords
      WriteStringN(Str(z) + " " + UniqueWords(z) + Chr(9) + Chr(9) + Str(WordCount(z)))
    Next
    CloseFile(0)
  EndIf
  
  ShellExecute_(hWnd,"open","result.txt","","",#SW_SHOWNORMAL)
  
EndProcedure
  


;-----------------------------------------------------------------------------------------------------
;- Main Start
;-----------------------------------------------------------------------------------------------------

Quit.l = #False
WindowXSize.l = 320
WindowYSize.l = 240

CurrentDirectory = Space(255)
GetCurrentDirectory_(255, @CurrentDirectory)
  
EOL.s = Chr(13) + Chr(10)
  
For I = 0 To 255
  AsciiConv(I) = Chr(I)
Next
  
AsciiConv(Asc(".")) = " "
AsciiConv(Asc(",")) = " "
AsciiConv(Asc(":")) = " "
AsciiConv(Asc(";")) = " "
AsciiConv(Asc("+")) = " "
  ;AsciiConv(Asc("-")) = " "
AsciiConv(Asc("_")) = " "
AsciiConv(Asc("*")) = " "
AsciiConv(Asc("/")) = " "
AsciiConv(Asc("(")) = " "
AsciiConv(Asc(")")) = " "
AsciiConv(Asc("[")) = " "
AsciiConv(Asc("]")) = " "
AsciiConv(Asc("'")) = " "
AsciiConv(Asc("!")) = " "
AsciiConv(Asc("?")) = " "
AsciiConv(Asc("{")) = " "
AsciiConv(Asc("}")) = " "
AsciiConv(Asc("=")) = " "
AsciiConv(Asc("<")) = " "
AsciiConv(Asc(">")) = " "
AsciiConv(Asc(Chr(34))) = " "
AsciiConv(Asc(Chr(9))) = " "
AsciiConv(Asc(Chr(171))) = " "
AsciiConv(Asc(Chr(187))) = " "
  
hWnd.l = OpenWindow(0, 200, 200, WindowXSize, WindowYSize, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar, "MyWindow")

If hWnd
  
  AddKeyboardShortcut(0, #PB_Shortcut_Escape, 99)
  LoadFont(0, "Verdana", 10)
  FontID.l = FontID()
  
  If CreateMenu(0, WindowID())
    OpenSubMenu("General")
    MenuItem(11, "Open file")
    MenuItem(99, "Quit")
    CloseSubMenu()
  EndIf
  
  If CreateStatusBar(0, WindowID())
    StatusBarText(0, 0, "Idle ...", 0)
  EndIf
  
  If CreateGadgetList(WindowID())
    SetGadgetFont(#PB_Default,FontID);moi
    TextGadget(100, 10, 10, WindowXSize - 20, WindowYSize - 40, "")
    SetGadgetText(100, "Select a file to process ...");moi
  EndIf
  
  Repeat
    
    Select WaitWindowEvent()
      
      Case #PB_EventCloseWindow
        Quit = #True
      
      Case #PB_EventMenu
        
        Select EventMenuID()
          
          Case 11
            
            ;SelectTheFile()
            
            Filename.s = OpenFileRequester("Select a file", CurrentDirectory + "\" + "*.txt", "Text files|*.txt|All files|*.*", 0, #PB_Requester_MultiSelection)
            NLines.l = 0
            NWords.l = 0
            tz.l = GetTickcount_()
            
            Debug "filename_bis" + Filename
            ParseFile(Filename)
  
            SortArray(AllWords(), 0, 0, NWords)
            
            CreateUniqueWordList()
            
            SetGadgetText(100, "File : " + Filename + EOL + "Lines : " + Str(NLines) + EOL + "Words : " + Str(NWords + 1) + EOL + "Unique words : " + Str(NUniqueWords + 1) + EOL + "Done in " + Str(GetTickcount_() - tz) + " ms")
 
            CreateWordFile()
            
          Case 99
            Quit = #True
        
        EndSelect
    
     EndSelect
  
   Until Quit

  EndIf

End
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

Works fine here, maybe it's the file you are using ? What size is it ?

I tried 200K text file... :)
Paid up PB User !
Fiwip
User
User
Posts: 27
Joined: Tue Feb 24, 2004 2:42 pm

Post by Fiwip »

dontmailme:

Thanks for testing and reply. I don't know what to say. I copy and paste my code from the post and it works in PB 3.89 b2 ! Whereas it did not work before when I copy and paste from PB 3.89 b2 in the post ...

Anyhow thanks again.

May be all my silly things can be erase from the forum as they are useless ?

Fp
Post Reply