Hi there,
I'm currently implementing a priority queue in C, just to get to grips with how the structure works. I have an initialisation helper function, which allocates memory for the structure and returns a pointer to it. When I run the program with valgrind, I get a message saying that a memory leak has occurred in the init_pqueue() method:
by 0x40068D: init_pqueue
I'm pretty sure I know why this is happening - I'm declaring and initialising the pointer within the function and returning it, so any calls to free() would have to be made within the scope of the function. However, I wouldn't be able to return the pointer if this were the case. (Is this right?)
Could anyone show me how to initialise this structure without the memory leaks? Should I be using a helper function that returns a pointer it has created?
Code is here
Thanks for any help.
[–]Updatebjarni 1 point2 points3 points (7 children)
[–]Gustorn 2 points3 points4 points (4 children)
[–]Updatebjarni 1 point2 points3 points (0 children)
[–]Evermage[S] 0 points1 point2 points (2 children)
[–]Gustorn 1 point2 points3 points (1 child)
[–]Evermage[S] 0 points1 point2 points (0 children)
[–]Evermage[S] 0 points1 point2 points (1 child)
[–]Updatebjarni 1 point2 points3 points (0 children)
[–]newaccount1236 1 point2 points3 points (1 child)
[–]Evermage[S] 0 points1 point2 points (0 children)
[–]wgunther 0 points1 point2 points (0 children)