
Bon je ne sais pas si vous avez suivi les episodes precedents de "KCC et le tableau magique".
C'est l'histoire d'un tableau que j'avais et "Pffffoouuuuf !!" il disparait
A coté de KCC .....COPPPERFIELD c'est un cornichon

Enfin sinon, c'est pas grave.
J'ai donc voulu passer des tableaux de strings dynamiques d'une procedure de DLL à un EXE.
Plein d'amis, m'ont aidé et j'ai donc obtenu plusieurs methodes.
Kcc il a été obligé de choisir une, qui lui parait le plus adapté à son niveau de connaissance.
Et ..."c'est encore un militaire qui gagne une tringle a rideau" (Coluche)

Donc, je vais utiliser la super methode de mon ami Cpl, car deja je l'ai comprise, apres qu'il me l'ai expliqué sur trois jours.
Et comme il le dit, elle est universelle, et elle apprend a gerer la memoire.
Enfin bref, .....je la sent

Mais voila, pendant mes perigrinations, XOMBIE m'a dit que je ne gerait pas correctement la memoire, quand j'ai presenté le code de Cpl modifié sauce KCC

J'ai cherché a faire comme il dit, mais y'a des << et des >> des SizeOff, enfin bref, un chouilla , pour ne pas dire un BIG chouilla au dessus des connaissances de KCC

Donc ma question est....est ce que mon code a vraiment besoin d'etre peaufiné au niveau de la mémoire (car il marche comme ça, mais bon ....)
Voici mon code
Code : Tout sélectionner
; http://www.purebasic.fr/french/viewtopic.php?p=101521#101521
ProcedureDLL.l CreateArray(Parametre)
Static Passage, xx, *PointerString
Passage + 1
If Passage = 1
*PointerString = AllocateMemory(100000 * 4); Tableau de pointeur
If Parametre = 1
Lettre.s = "a"
Else
Lettre.s = "c"
EndIf
Else
If Parametre = 2
Lettre.s = "b"
Else
Lettre.s = "d"
EndIf
EndIf
For x = 1 To 4
xx + 1
LenString = Len(RSet("", xx, Lettre))
*String = AllocateMemory(LenString * 4)
PokeS(*String, RSet("", xx, Lettre))
PokeI(*PointerString + (xx * 4), *String)
Next
If Passage = 1
CreateArray(Parametre)
ReAllocateMemory(*PointerString, (xx * 4) + 4)
Passage = 0
xx = 0
EndIf
PokeI(*PointerString, #Null)
ProcedureReturn *PointerString
EndProcedure
Dim ArrayA.s(0)
ArrayA() = CreateArray(1)
Dim ArrayB.s(0)
ArrayB() = CreateArray(2)
For i = 1 To (MemorySize(ArrayA()) / 4) - 1
Debug "Premier essai = " + ArrayA(i)
Next
For i = 1 To (MemorySize(ArrayB()) / 4) - 1
Debug "Second essai = " + ArrayB(i)
Next
Je vous remercie de votre aideXombie a écrit :Kwai,
Change this part in your program.
And notice what happens. You're not creating an array. You're creating a memory block.Code : Tout sélectionner
Dim ArrayA.s(10) Debug "Old Array address: "+Str(@ArrayA()) Debug "Old array item count (using ArraySize): "+Str(ArraySize(ArrayA())) Debug "Old array item count (using peek): "+Str(PeekL(@ArrayA() - 8) ) ArrayA() = CreateArray() Debug "New 'Array' address: "+Str(@ArrayA()) Debug "New array item count (using peek): "+Str(PeekL(@ArrayA() - 8) ) Debug "New array item count (using memory size): "+Str(MemorySize(@ArrayA()) >> 2)
Now the question becomes - do you have to have an array or can you use a block of memory that thinks it's an array?
Maybe you should rethink what you're doing. Sometimes in programming it's easy to get stuck thinking you *have to* do something one way.
First, I think this line is in error:
As it should be adding one to xx. You want one more item, right? If you add it after you multiple times 4 then you'll get 41 bytes of memory rather than 44 and that can't hold 11 items.Code : Tout sélectionner
ReAllocateMemory(*PointerString, (xx * 4) + 1)
Use:The '<< 2' is a little faster as a bit shift as well.Code : Tout sélectionner
ReAllocateMemory(*PointerString, ((xx + 1) << 2))
Also, I don't know what the extra 'CreateArray()' call does in your CreateArray() procedure.
Also, you're still only allocating one memory address and assigning it to your two different 'arrays'.
Well, in fact you have quite a few errors in pointers and such so I cleaned up your code for what it looked like you were doing. Especially since it seemed like you were trying to enable it for 32 and 64 bit operations. Try the following code...
Code : Tout sélectionner
Procedure FreePBString(*Address) ; Procedure and method of removing structure strings from memory from freak @ http://www.purebasic.fr/english/viewtopic.php?t=23099&start=0 Protected String.String ; PokeL(@String, *Address) ; EndProcedure ProcedureDLL.l CreateArray(Which.i) ; Define.i xx, *PointerString ; *PointerString = AllocateMemory(100000 << ((SizeOf(Integer) >> 2) + 1)); Tableau de pointeur ; If Which = 1 Lettre.s = "a" Else Lettre.s = "b" EndIf ; For x = 1 To 5 LenString = Len(RSet("", x, Lettre)) *String = AllocateMemory((LenString << (SizeOf(Character) - 1)) + SizeOf(Character)) PokeS(*String, RSet("", x, Lettre)) PokeI(*PointerString + ((x - 1) << ((SizeOf(Integer) >> 2) + 1)), *String); ((x - 1) << (SizeOf(Integer) >> 2)), *String) xx + 1 Next ; *PointerString = ReAllocateMemory(*PointerString, xx << ((SizeOf(Integer) >> 2) + 1)) ; ProcedureReturn *PointerString ; EndProcedure *MemoryA = CreateArray(1) *MemoryB = CreateArray(2) SizeA.i = MemorySize(*MemoryA) >> 2 Debug SizeA SizeB.i = MemorySize(*MemoryB) >> 2 Debug SizeB For i = 1 To SizeA Debug "Premier essai = " + PeekS(PeekI(*MemoryA + ((i - 1) << ((SizeOf(Integer) >> 2) + 1)))) Next For i = 1 To SizeB Debug "Premier essai = " + PeekS(PeekI(*MemoryB + ((i - 1) << ((SizeOf(Integer) >> 2) + 1)))) Next Debug "Free A..." For i = 1 To SizeA FreePBString(PeekI(*MemoryA + ((i - 1) << ((SizeOf(Integer) >> 2) + 1)))) Next Debug "Free B..." For i = 1 To SizeB FreePBString(PeekI(*MemoryB + ((i - 1) << ((SizeOf(Integer) >> 2) + 1)))) Next FreeMemory(*MemoryA) FreeMemory(*MemoryB)
Bonne journée