Giter Site home page Giter Site logo

freeing curr-val before curr about list HOT 4 CLOSED

clibs avatar clibs commented on July 29, 2024
freeing curr-val before curr

from list.

Comments (4)

tj avatar tj commented on July 29, 2024

thanks! that LIST_FREE just frees the node itself which holds the value

from list.

humanshell avatar humanshell commented on July 29, 2024

Thanks for getting back to me so fast.

I think I understand. You need the the call to if (self->free) self->free(curr->val); because the value associated with a node (its data) is stored separately in memory? So freeing the node only frees the struct? If you didn't have if (self->free) self->free(curr->val); then you'd have a memory leak?

from list.

tj avatar tj commented on July 29, 2024

yup that's correct, the general rule is every allocation needs a mirrored free()

from list.

humanshell avatar humanshell commented on July 29, 2024

Awesome, very cool.

I've always known that manual memory management almost always becomes a headache. But I'm only now beginning to get the chance to work with C on a level that requires dynamic memory from the heap (malloc/free). I'm starting to see now why garbage collection is so popular. :-)

Because list_node_new() accepts a void* to a val somewhere in memory, it's your library's responsibility to ensure the memory created (no matter who allocated and passed it in) gets properly free()'d when the list is no longer needed.

Thanks again for the assistance.

from list.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.