Page 1 of 1
Pointers, functions, arrays and such?
Posted: Thu Sep 20, 2018 3:23 pm
by Smetad_Anarkist
I'm working on a simple Queue, using an array and a couple of functions. It seems to work, I've set the type of the array to integer and the Enqueue functions signature is Enqueue(*state) and what I think I'm storing is the pointer to a structure with a couple of fields.
Is this actually a good solution? When my computations are done and I try to call FreeArray on my queue, the program chrashes, probably because I've misunderstood something regarding how pointers work and such. If I want to create a large amount of structures and store these in an array, is pointers the best choice and if so, how do I make sure I can dispose of all of them once I'm done?
Re: Pointers, functions, arrays and such?
Posted: Thu Sep 20, 2018 3:40 pm
by Fig
If your pointers point to an array which had been freed, it will crash of course. Pointers don't copy they just refere to something that exists.... Until it doesn't.
If you free something, be sure you don't use any old pointer to this deleted thing.
Not sure if it helps, it's difficult to answer more precisly without actual code.
About Queue, I would probably use a structured linkedlist for this purpose.
Re: Pointers, functions, arrays and such?
Posted: Thu Sep 20, 2018 3:49 pm
by NicTheQuick
According to your description you are doing nothing wrong. You should show use your code, so we can help your better with that.
Re: Pointers, functions, arrays and such?
Posted: Thu Sep 20, 2018 5:27 pm
by Smetad_Anarkist
Fig wrote:About Queue, I would probably use a structured linkedlist for this purpose.
Irregardless of making an queue out of an array or linked list, the problem still persists when freeing the memory. Also I am exploring the pros and cons of both methods, but I want to solve this issue first.
I'm also simplifying my code in these examples to focus on what I think are the problem areas.
The queue
Code: Select all
Dim queue.i(1)
Procedure Dequeue()
; I need to increment the head for the next call, but I can't do that once I've returned
head = head + 1
ProcedureReturn queue(head - 1)
EndProcedure
Procedure Enqueue(*item)
queue(tail) = *item
tail = tail + 1
EndProcedure
The actual using of the queue
Code: Select all
*myState.State = AllocateStructure(State)
; set the fields of the state ...
Queue::Enqueue(*myState)
While Not Queue::Empty()
*current.State = Queue::Dequeue()
*newState.State = AllocateStructure(State)
; Dim array and set fields
Dim *newState\arrayField(ArraySize(*current\arrayField)
CopyArray(*current\arrayField(), *newState\arrayField())
Queue::Enqueue(*newState)
If SolutionFound
; Break out of loop
EndIf
Wend
; Here I should probably do the freeing of the memory.
Does this make sense?
Re: Pointers, functions, arrays and such?
Posted: Thu Sep 20, 2018 6:34 pm
by #NULL
When my computations are done and I try to call FreeArray on my queue, the program chrashes
Do you FreeStructure the dequeued elements, i.e. *current? And is the queue really empty when you free it (i.e. the Break)? i guess those things missing would rather cause leak instead of crash though.
Is the arrayField just unrelated data or are you trying to store a queue pointer within the elements? Just asking, it looks more like queue is a plain integer/pointer array that doesn't claim any ownership of the elements, right?
Re: Pointers, functions, arrays and such?
Posted: Fri Sep 21, 2018 3:47 pm
by Smetad_Anarkist
The arrayField is a char array that I use to represent the data I'm working on, there are also a couple of integers and a string field as well.
The queue is probably not empty, to clear it I've added a loop that goes through it and does a FreeStructure on each element. Before calling the function to free the queue, I make sure to free both *current and *newState so the only references to the data should be the queue array itself.
Re: Pointers, functions, arrays and such?
Posted: Fri Sep 21, 2018 4:50 pm
by #NULL
If you FreeStructure *current/*new as well as each queue element then that's wrong because you will try to free pointers twice. It's also the question if you free only elements within the head/tail range or all elements including unused slots if you have those. In the example the queue didn't allocate so it should not free either. The caller code allocates and only dumps a pointer copy into the queue and the caller code should free it after having dequed it if it doesn't use it anymore.
Re: Pointers, functions, arrays and such?
Posted: Fri Sep 21, 2018 6:28 pm
by Smetad_Anarkist
Is there a way to see if a pointer has been free'd?
Also, I changed my FreeQueue loop to only go between the head and tail, and it has helped. About 50% of the ram is freed. So success, but maybe not all the way.
Re: Pointers, functions, arrays and such?
Posted: Fri Sep 21, 2018 8:07 pm
by #NULL
Smetad_Anarkist wrote:Is there a way to see if a pointer has been free'd?
The following code runs without crash here but with purifier enabled it will tell you the double-freeing
Code: Select all
; compiler options > compile/run > [x] enable purifier
PurifierGranularity(1,1,1,1)
Dim queue.i(10)
val = AllocateStructure(Integer)
queue(4) = val
Debug val
FreeStructure(val)
For i=0 To 10
If queue(i)
Debug queue(i)
FreeStructure(queue(i))
EndIf
Next
FreeArray(queue())