*MemoryBuffer

Just starting out? Need help? Post your questions and find answers here.
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

*MemoryBuffer

Post by rndrei »

Tell me how to read the right byte from memory?

Code: Select all

*buffer = AllocateMemory(1000)
byte=*buffer(10)
byte2=*buffer[50]
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: *MemoryBuffer

Post by Mijikai »

Example:

Code: Select all

EnableExplicit

Structure BYTE_STRUCT
  byte.b[0]
EndStructure

Procedure.i main()
  Protected.BYTE_STRUCT *buffer
  *buffer = AllocateMemory(1000)
  If *buffer
    *buffer\byte[10] = $FF
    
    Debug *buffer\byte[10]
    
    ShowMemoryViewer(*buffer,1000)
    
    FreeMemory(*buffer)
  EndIf
  ProcedureReturn #Null
EndProcedure

End main()
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: *MemoryBuffer

Post by infratec »

Read the help about AllocateMemory() and look at the example.
There you can find PokeS()
Then press at the bottom of the help on Memory durectory and look at the available commands.

Hint: look deeper at the Peek commands
Quin
Addict
Addict
Posts: 1122
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: *MemoryBuffer

Post by Quin »

I've told you this before and I'll tell you again:
Please go read the PB docs in-depth. Many programming languages struggle with a lack of documentation, PureBasic is not one of them. The english isn't always spotless but it's super well-written and clear documentation.
What you want are the peek/poke commands under the Memory section of the help file :) PeekB to peek (view) a byte, PeekS to peek a string, etc.
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: *MemoryBuffer

Post by Piero »

Mijikai wrote: Thu Apr 24, 2025 6:40 pm Example:
Thanks!

Peek Poke Array on Mac:
https://www.purebasic.fr/english/viewto ... 49#p633249
User avatar
Demivec
Addict
Addict
Posts: 4259
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: *MemoryBuffer

Post by Demivec »

rndrei wrote: Thu Apr 24, 2025 6:31 pm Tell me how to read the right byte from memory?

Code: Select all

*buffer = AllocateMemory(1000)
byte=*buffer(10)
byte2=*buffer[50]
The short answer is to do as Quin said and read the help manual instead of asking forum users to quote each page of it to you.

Here's a longer answer with no additional explanation:

Code: Select all

*buffer = AllocateMemory(1000)
byte = PeekB(*buffer + 10)
byte2 = PeekB(*buffer + 50)
LifeUniverseNEverything = (Bool(PeekB(*buffer + '*') > '*' ) +
                           Bool(PeekB(*buffer + '*') < '*' ) +
                           Bool(PeekB(*buffer + '*') = '*' )) * '*'
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: *MemoryBuffer

Post by Piero »

Why are you gurus so angry @rndrei?
Sometimes (even if you know a bit of english!) it's not very easy to browse help………

PS: rndrei, did you see that you can also use the "PureBasic documentation in PDF format (full version)" to search stuff?
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: *MemoryBuffer

Post by jacdelad »

Because he found AllocateMemory which includes an example which should lead you to the right way. :wink:
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: *MemoryBuffer

Post by Piero »

jacdelad wrote: Fri Apr 25, 2025 8:36 am Because he found AllocateMemory which includes an example which should lead you to the right way. :wink:
Apropos: in my tests (but I tried on win 11 arm VM) the "array" method is always faster than peek/poke
https://www.purebasic.fr/english/viewto ... 49#p633249
User avatar
Demivec
Addict
Addict
Posts: 4259
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: *MemoryBuffer

Post by Demivec »

Piero wrote: Fri Apr 25, 2025 8:33 am Why are you gurus so angry @rndrei?
Sometimes (even if you know a bit of english!) it's not very easy to browse help………
The help file is available in German and French as well. In addition, the online html version can be translated into other languages with the Chrome web browser's 'translate' option for web pages.
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: *MemoryBuffer

Post by Piero »

Demivec wrote: Fri Apr 25, 2025 9:08 am'translate' option for web pages.
I guess you think so because your native language is english, so the automatic "translations" are generally decent; if that's the case, I can assure you that if you were, e.g., italian, you would "think VERY different" (especially if it involves pineapple pizzas)
User avatar
Demivec
Addict
Addict
Posts: 4259
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: *MemoryBuffer

Post by Demivec »

Piero wrote: Fri Apr 25, 2025 9:36 am
Demivec wrote: Fri Apr 25, 2025 9:08 am'translate' option for web pages.
I guess you think so because your native language is english, so the automatic "translations" are generally decent; if that's the case, I can assure you that if you were, e.g., italian, you would "think VERY different" (especially if it involves pineapple pizzas)
Languages and writing styles come in many flavors and are used in different and sometimes dicey levels of fluency. I mention automatic translations only as an additional option to having no translation. When I started with PureBasic the English version of the help file had many issues due to it being written by non native speakers. I survived and it has improved over the years. In addition to English I also communicate in German, French, and American Sign Language and have been a student of other languages including Spanish, Russian, Mandarin, and Japanese and would study many more if not for the lack of time. Some things do not translate well between different languages but a rough translation is often better than no translation.
Speaking of pineapple pizzas, I ate pineapple pizza con gusto for dinner. They're one of my favorites, among many others of course. :)
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: *MemoryBuffer

Post by Mijikai »

Playing around:

Code: Select all

EnableExplicit

Macro CharBuffer(_buffer_);<- defines cmem!
  !char* cmem = (char*)p_#_buffer_;
EndMacro

Macro CharSet(_index_,_value_)
  !cmem[_index_] =  _value_;
EndMacro

Macro CharGet(_index_,_byte_)
  !v_#_byte_ = cmem[_index_];
EndMacro

Procedure.i main()
  Protected *buffer
  Protected.b byte
  *buffer = AllocateMemory(1000)
  If *buffer
    CharBuffer(buffer)
    CharSet(10,255)
    CharGet(10,byte)
    Debug byte
    ShowMemoryViewer(*buffer,1000)
    FreeMemory(*buffer)
  EndIf
  ProcedureReturn #Null
EndProcedure

End main()
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

Re: *MemoryBuffer

Post by rndrei »

Thanks for the different options, but I have never found documentation about reading the right byte from the buffer!
And I drank cappuccino!
Post Reply