(SOLVED) Memory+wstring

New to FreeBASIC? Post your questions here.
Post Reply
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

(SOLVED) Memory+wstring

Post by VANYA »

Hi all!

I'm a little confused... Tell me, is the code below reasonable or is it problematic?

Code: Select all

dim as wstring ptr w = callocate(1) ' memory for 1 byte !!!!!!!!!!!!!!!!!!

for i as Long = 32 to 255
	
	*w &=wchr(i)
	
Next
If everything is fine, then it seems that memory does not need to be released (this will be done by the compiler)?
Last edited by VANYA on Apr 27, 2022 17:51, edited 1 time in total.
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Memory+wstring

Post by SARG »

Problematic. Your code is not working, try print *w.

And the example from manual is and

Code: Select all

Dim As WString Ptr str2 = Allocate( 13 * Len(WString) )
*str2 = "hello, world"
Print *str2
Print Len(*str2)  
sleep
And deallocate should preferably also used.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Memory+wstring

Post by VANYA »

SARG wrote: Apr 27, 2022 10:34 Problematic. Your code is not working, try print *w
SARG!

I wouldn't ask if the example didn't output anything, but the example outputs the correct information and doesn't even crash:

Here is the output result:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ
žŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
But however, if you increase the size of the counter in the loop to 5000, then a crash appears. And if so, then everything works correctly:

Code: Select all

dim as wstring ptr w = callocate(1) ' memory for 1 byte !!!!!!!!!!!!!!!!!!

for i as Long = 32 to 4000
	
	*w &=wchr(i)
	
Next

? *w
It seems that the amount of free memory turned out to be rather big and I wrote to this memory. Lucky :)
I started to think that somehow the compiler was internally allocating memory for this. Now everything is logical, the question is settled.

Thanks for the reply.
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Memory+wstring

Post by fxm »

The correct code for the first post:

Code: Select all

dim as wstring ptr w = callocate((255-32+1) + 1, sizeof(wstring))  '' don't forget the terminal character

for i as Long = 32 to 255
	*w &=wchr(i)
next

print *w

deallocate(w)

sleep
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Memory+wstring

Post by SARG »

@VANYA,
Not at home so a late reply.
My test was on Windows with the code below and nothing was printed so a crash.
Maybe Linux gives more memory. ;-)

Code: Select all

dim as wstring ptr w = callocate(1) ' memory for 1 byte !!!!!!!!!!!!!!!!!!

for i as Long = 32 to 255
	
	*w &=wchr(i)
	
Next
print "before"
print *w
print "end"
sleep

Edit : rerunning the test it passes without problem so it's very randomly.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Memory+wstring

Post by VANYA »

SARG wrote: Apr 27, 2022 15:29 Edit : rerunning the test it passes without problem so it's very randomly.
yes, but on Linux, an example from 32 to 4000 always works fine for me. Although, logically, it should crash :)

@fxm thanks for joining the discussion.

I'll mark the topic as solved in the title. I won't be following the topic anymore. So, if someone else will write, do not be offended if I do not answer.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: (SOLVED) Memory+wstring

Post by caseih »

If not a crash, certainly I would expect memory corruption. You're writing past the end of a buffer, so something is getting clobbered. This is probably the most common bug in all of computer programming history, and the cause of most security flaws.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: (SOLVED) Memory+wstring

Post by D.J.Peters »

@caseih If the loader from the OS loads the app, a part of "virtual" memory are "pre" allocated (used or not). If you write over a self allocated buffer range (in real a bunch of allocated pages of 4/8/16KB each) does not crash your app at all.
Or imagine you allocate 255 bytes but the smallest page in the cache memory are 16KB nothing critical are heppent.

Of course the computer system of a 747 or nuclear power plant notice any out of bounds write or read access :-)

(I hope so :lol:)

Joshy
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: (SOLVED) Memory+wstring

Post by dodicat »

The help file says for reallocate:
Attempts to reallocate, or resize, memory previously allocated with Allocate or CAllocate.
So I assume it is a hit or miss affair, otherwise you could provide memory on the fly if you didn't know exactly how much is needed to begin with, if you worked in a power station say? if you were a pilot say?

Code: Select all

randomize
dim as wstring ptr w = callocate(sizeof(wstring)) ' starting slot !!!!!!!!!!!!!!!!!!

dim as ulong limit=5000+rnd*1000
for i as Long = 32 to limit
	w=reallocate(w,(i+1)*sizeof(wstring))
	*w &=wchr(i)
Next
open "wstrings.txt" for output  Encoding "utf-8" as #1
print #1, *w
print #1, limit
close #1
shell "notepad wstrings.txt"
deallocate w
print "done"
sleep
kill "wstrings.txt"
 
Last edited by dodicat on Apr 28, 2022 8:34, edited 1 time in total.
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: (SOLVED) Memory+wstring

Post by fxm »

dodicat wrote: Apr 28, 2022 8:13 The help file says for reallocate:
Attempts to reallocate, or resize, memory previously allocated with Allocate or CAllocate.
So I assume it is a hit or miss affair, ...
The attempt relates to the fact of first trying to resize the memory already allocated without modifying its starting address in order to avoid recopying the data to be kept (value of the return pointer unchanged).
If not possible, a new separate memory area is allocated with recopy of the data to be kept before deallocating the old one (return pointer value changed).
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: (SOLVED) Memory+wstring

Post by SARG »

D.J.Peters wrote: Apr 28, 2022 3:30 If you write over a self allocated buffer range (in real a bunch of allocated pages of 4/8/16KB each) does not crash your app at all.
Or imagine you allocate 255 bytes but the smallest page in the cache memory are 16KB nothing critical are heppent.
Not so sure, only 'begin' is displayed then crash. Sometimes this message saying that the memory address can't be read.
But as I wrote that happens randomly.
Image
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: (SOLVED) Memory+wstring

Post by fxm »

fxm wrote: Apr 28, 2022 8:33
dodicat wrote: Apr 28, 2022 8:13 The help file says for reallocate:
Attempts to reallocate, or resize, memory previously allocated with Allocate or CAllocate.
So I assume it is a hit or miss affair, ...
The attempt relates to the fact of first trying to resize the memory already allocated without modifying its starting address in order to avoid recopying the data to be kept (value of the return pointer unchanged).
If not possible, a new separate memory area is allocated with recopy of the data to be kept before deallocating the old one (return pointer value changed).

@dodicat and the others:

Would it be helpful to clarify/precise this in the REALLOCATE documentation page ?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: (SOLVED) Memory+wstring

Post by dodicat »

Sure thing fxm.
The word attempt could mean
try
strive
aim
venture
endeavour
seek
set out
do one's best
do all one can
do one's utmost
make an effort
make every effort
spare no effort
give one's all
take it on oneself
have a go at

So that would clear things up.
Thank you.
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: (SOLVED) Memory+wstring

Post by fxm »

Update of REALLOCATE documentation page done:
- KeyPgReallocate → fxm [rewording]
Post Reply