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?
Pointers, functions, arrays and such?
-
- New User
- Posts: 8
- Joined: Wed Jul 19, 2017 5:50 am
Re: Pointers, functions, arrays and such?
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.
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.
There are 2 methods to program bugless.
But only the third works fine.
Win10, Pb x64 5.71 LTS
But only the third works fine.
Win10, Pb x64 5.71 LTS
- NicTheQuick
- Addict
- Posts: 1504
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Pointers, functions, arrays and such?
According to your description you are doing nothing wrong. You should show use your code, so we can help your better with that.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
-
- New User
- Posts: 8
- Joined: Wed Jul 19, 2017 5:50 am
Re: Pointers, functions, arrays and such?
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.Fig wrote:About Queue, I would probably use a structured linkedlist for this purpose.
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
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.
Re: Pointers, functions, arrays and such?
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.When my computations are done and I try to call FreeArray on my queue, the program chrashes
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?
-
- New User
- Posts: 8
- Joined: Wed Jul 19, 2017 5:50 am
Re: Pointers, functions, arrays and such?
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.
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?
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.
-
- New User
- Posts: 8
- Joined: Wed Jul 19, 2017 5:50 am
Re: Pointers, functions, arrays and such?
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.
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?
The following code runs without crash here but with purifier enabled it will tell you the double-freeingSmetad_Anarkist wrote:Is there a way to see if a pointer has been free'd?
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())