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
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
Code: Select all
Dim As WString Ptr str2 = Allocate( 13 * Len(WString) )
*str2 = "hello, world"
Print *str2
Print Len(*str2)
sleep
SARG!
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:!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
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
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
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
yes, but on Linux, an example from 32 to 4000 always works fine for me. Although, logically, it should crash
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"
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).
Not so sure, only 'begin' is displayed then crash. Sometimes this message saying that the memory address can't be read.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.
fxm wrote: ↑Apr 28, 2022 8:33The 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).