Giter Site home page Giter Site logo

Function pointers about list HOT 3 CLOSED

clibs avatar clibs commented on July 29, 2024
Function pointers

from list.

Comments (3)

tj avatar tj commented on July 29, 2024

the .free field doesn't actually have anything to do with free() itself, just happens to be the name of that field, but the reason for it is so that you can assign the function that should be called to clean up all the node values, instead of iterating/freeing them all manually. There's no function to assign or anything though, just like this https://github.com/visionmedia/list/blob/master/test.c#L130. Likewise here's an example .match https://github.com/visionmedia/list/blob/master/test.c#L130 so you can filter values by some custom function

from list.

humanshell avatar humanshell commented on July 29, 2024

OMG! I totally get it. They're NULL 'stubs' or 'placeholders'.

It dawned on me that looking at your test.c could provide some insight to how those fields were intended to be used. I was starting to understand when your reply came in and that just confirmed it. It's actually REALLY cool, providing an empty 'plug' where a consumer of your library or api can insert their own custom function to handle cleanup and comparisons. Very nice.

It just leaves me with two questions about the list_find() and list_remove() functions:

1.) both the list_remove() and list_destroy() functions call the .free field, but they're not the same. destroy calls self->free(curr->val) and remove calls self->free(node). Is there a reason these calls differ? The former passes the current node's value while the latter passes the node itself. I'm thinking that remove is just a singular form of destroy, shouldn't these two methods pass similar arguments to the .free field?

2.) list_find() is the only function using the .match field. I'm confused by the conditional fallback. If no function has been attached to the .match field, then a simple '==' comparison is performed on the passed in val and the current node's val. Won't this comparison always fail because it's comparing pointer values? The passed in val points to one thing and the current node's val points to something else. While the data in memory may be the "same" won't those pointer addresses always be different, causing the comparison to always return false?

from list.

jonathanmarvens avatar jonathanmarvens commented on July 29, 2024

Closing this issue as solved (given it wasn't an issue anyway).

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.