How many extra bytes are in the string?

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 2223
Joined: Sun May 14, 2017 1:48 am

How many extra bytes are in the string?

Post by AZJIO »

I have heard that when creating a string, a little more bytes are allocated than necessary, so when adding a pair of CRLF characters to a string, there is no memory overallocation (ReAllocateMemory) and it works quickly. I'm wondering how many bytes are allocated more than required. How many characters can I add to a string without causing memory overload.
User avatar
skywalk
Addict
Addict
Posts: 4241
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How many extra bytes are in the string?

Post by skywalk »

Ouch, that is not guaranteed behavior. :evil:
You must assume your string byte length is accurate and not use "behind the scenes" padding.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
AZJIO
Addict
Addict
Posts: 2223
Joined: Sun May 14, 2017 1:48 am

Re: How many extra bytes are in the string?

Post by AZJIO »

skywalk wrote: Thu May 22, 2025 8:20 pm and not use "behind the scenes" padding.
This will be the standard method

Code: Select all

strind + #CRLF$ + #TAB$
But I want to make sure that +#TAB$ doesn't cause memory over-allocation.

I want to know in which case I can use the normal method and in which case I have to use the CopyMemoryString() function.
Last edited by AZJIO on Thu May 22, 2025 9:06 pm, edited 1 time in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How many extra bytes are in the string?

Post by mk-soft »

It is requested as much memory as one needs and goes coherently.

You can also forcibly release the string memory ;)

Start without debugger as console and watch memory

Code: Select all

Define i.i, s.s

mb = 1024*1024*16

If OpenConsole()
  i + 1
  PrintN("" + i + ": no string")
  Input()
  i + 1
  s = Space(mb)
  PrintN("" + i + ": 16mb unicode string")
  Input()
  i + 1
  s + Space(mb)
  PrintN("" + i + ": plus 16mb unicode string")
  Input()
  i + 1
  PrintN("" + i + ": release string")
  s = #Null$
  Input()
  
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: How many extra bytes are in the string?

Post by SMaag »

I struggled with this at my early Assembler tests with Strings. At the beginning I did not understand what are the values after the end of the String.

The allocation is 4 Byte aligned at x86 and 8 Byte aligend at x64. So number of charcters * SizeOf(Charcter) + CHR(0) + FillBytesForMemoryAling
So you can write without problems until the end of the aligned size. At x64 and Charsiez=2 this are maximal 3 characters more than you String.
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How many extra bytes are in the string?

Post by mk-soft »

PureBasic decides internally how much memory is reserved behind the string ...

Code: Select all

Define a$

For i = 1 To 100
 a$ + "x"
 Debug "pString = " + @a$
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply