Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^8: Inline::C self-referential struct idioms and memory

by rutgeraldo (Novice)
on Sep 04, 2015 at 21:28 UTC ( [id://1141057]=note: print w/replies, xml ) Need Help??


in reply to Re^7: Inline::C self-referential struct idioms and memory
in thread Inline::C self-referential struct idioms and memory

Subsequent to this new convention I have been doing some more computer programming and have come to learn that in (Safe)free()-ing variables there is a difference between ones that are "automatically" allocated and ones that are "dynamically" allocated. I am told that the difference is that the former are allocated on the stack, the latter on the heap. Apparently you can only free() dynamically allocated ones on the heap; doing it on the other ones triggers the error. I guess I am doing that inside the destructor. In any case, commenting out the Safefree makes the error go away, for better or worse.

  • Comment on Re^8: Inline::C self-referential struct idioms and memory

Replies are listed 'Best First'.
Re^9: Inline::C self-referential struct idioms and memory
by syphilis (Archbishop) on Sep 05, 2015 at 01:30 UTC
    Apparently you can only free() dynamically allocated ones on the heap

    AIUI, when you create using Newx(), you are allocating dynamically - and you'll leak memory if you don't Safefree() those dynamically allocated variables when they go out of scope.

    Incidentally, DESTROY() doesn't have to be a perl sub.
    You could dispense with that perl sub and place it in the C code as something like (untested):
    void DESTROY(Node* self) { warn("destroying self"); Safefree(self); }
    However, you may well find that there are additional things to Safefree (again untested):
    void DESTROY(Node* self) { warn("destroying self"); Safefree(self-> parent); Safefree(self-> sv); Safefree(self); }
    Admittedly, you've then lost the identification of the actual object that's being Safefree'd - though I think that could be easily addressed.
    And I'm not sure how the self-referential nature of the Node object impacts upon things.

    Cheers,
    Rob
Re^9: Inline::C self-referential struct idioms and memory
by BrowserUk (Patriarch) on Sep 04, 2015 at 21:34 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1141057]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found