Thread: Functions as structure members.

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    104

    Functions as structure members.

    Hi guys,
    I’m still dabbling with graphics, this time working on a fractal explorer and today, on a whim, I came up with this absurd alternative to using an array of function pointers:
    Code:
    // On a header file: 
    
    typedef struct    s_fractal
    {
        ...
        ...
        unsigned int  (*compute)(struct s_fractal *, t_i2dcoordinate, unsigned int);
        ...
        ...
    }                 t_fractal;
    
    //On the fractal calculator source file. Above header file is included for the t_fractal type.
    
    static unsigned int compute_julia
    (t_fractal *fractal, const t_i2dcoordinate screen_coordinates,
    const unsigned int max_iterations)
    {
        ...
        ...
        return (iterations);
    }
    
    static const t_fractal_params        parameters =
    {
        {
            ...
            {&compute_julia, "julia", {-2.5, -1}, {1, 1}, {0.7, 0}, (-0.743643f +  0.131825f * I)},
            ...
        }
    };
    
    extern void    initialize_fractal(t_fractal *fractal, const char *fractal_name /*coming from argv*/)
    {
        ...
        while (strcmp(fractal_name, parameters.fractals[id].name))
            ++id;
        ...
        (*fractal).compute = parameters.fractals[id].compute;
        ...
    }
    
    // Finally, on my rendering source file:
    
    static  void    *resolve_fractal(void *thread)
    {
        t_workset    *workset = (*(t_thread_data*)thread).workset;
        t_fractal    *fractal = (*workset).fractal;
        ...
        ...
        iterations = (*fractal).compute(fractal, screen_coordinates);
        (*workset).framebuffer[.....] = get_pixel_color(iterations);
        ...
        ...
        pthread_exit((void*)0);
    }
    And it works!
    I like it much better than my usual array of function pointers solution, because for one I don't have to deal with arrays and more importantly, using the "object.function" syntax seems neater to me.
    Now, I have great uncertainty on whether this is dangerous, results in undefined behavior, is violating all the iso standards or can make my toaster explode.

    resolve_fractal is a static function, and so not visible to the outside world. Yet I am using it in the ouside world, exclusively. Does the compiler generate symbols for this function? What if I declare it inline? (I tried and it works all the same). How would compiling with -flto change things?
    I'd greatly appreciate your take on this?

    Thank you.
    printf("I'm a %s.\n", strrev("Dren"));

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Does the compiler generate symbols for this function?
    Only the static symbols apparently.

    > What if I declare it inline? (I tried and it works all the same).
    But you still end up by calling the non-inlined version anyway, because elsewhere you took it's address.

    > How would compiling with -flto change things?
    It wouldn't (maybe).
    The relationship between resolve_fractal and say compute_julia (despite the different signatures in your précis) is a run-time choice.

    Constructing a static call graph and optimising the code between the functions is one thing.

    Peering through a table of function pointers chosen at run-time is another.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    104
    Salem, you are a dear

    Second:
    resolve_fractal is a static function, and so not visible to the outside world. Yet I am using it in the outside world, exclusively
    I meant resolve_julia. I mixed up quite a few things when transplanting my code, including a few identifiers I changed along the way, a curse that plagues my productive hours: the everlasting quest of choosing apt identifiers and changing them every time I re-read the code.

    I also scrapped much of my foreword knowing that I am overly verbose, and wanting to avoid an unnecessary preamble, I ended up removing perhaps valuable context.

    In essence, the work starts in my rendering function which creates four threads of resolve_fractal, each looping over different screen coordinates and feeding them to fractal.compute. Fractal being a a pointer to a shared fractal object, and compute pointing to the respective fractal calculation function, which I determine when initializing the object, at the start of the program.

    I must apologize as I seem to have committed the cardinal sin of asking for information that is ready available on the internet -although Salem did provide valuable insight I wouldn’t have easily found otherwise; googling the very name of my thread turns out plenty of results indicating I did not discover anything new, this seems to be a well known concept which I aim to study in detail. I might appear disingenuous but that is far from the case. I honestly had no inkling.

    Moral of the story: search before posting kids, google is your friend* and eat your damned vegetables.




    * More like your enemy, literally worse than Microshaft. duckduckgo, startpage ftw! And don’t forget to switch to Tor, abandon all ME, PSP infested CPUs made in the last decade and rock libreboot. My, I am rambling at this point, I’ll see myself out.
    printf("I'm a %s.\n", strrev("Dren"));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-26-2018, 06:04 AM
  2. How to find number of structure members in a given structure?
    By bhaskarReddy in forum C Programming
    Replies: 4
    Last Post: 01-16-2012, 05:37 AM
  3. Accessing Structure Members
    By anndruu12 in forum C Programming
    Replies: 5
    Last Post: 12-02-2010, 02:37 AM
  4. structure members outside main
    By threahdead in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 07:34 AM
  5. Accessing structure members
    By foniks munkee in forum C Programming
    Replies: 18
    Last Post: 02-13-2002, 03:06 PM

Tags for this Thread