Author Topic: Is function in struct efficient in c?  (Read 5376 times)

0 Members and 1 Guest are viewing this topic.

Offline bd139

  • Super Contributor
  • ***
  • Posts: 23018
  • Country: gb
Re: Is function in struct efficient in c?
« Reply #25 on: November 11, 2017, 06:15:14 am »
I think the only time I use a function pointer these days is whe I want to inject a custom allocator into a library I’ve written. I’ll have a function which takes malloc/free function pointer which overrides the default implementation if called.

I’ll only use C if I’m writing a library that needs to be pulled in from other languages (python in particular) where it’s a shit ton easier using it with FFI systems.

Anything greater calls for C++
 

Offline Feynman

  • Regular Contributor
  • *
  • Posts: 192
  • Country: ch
Re: Is function in struct efficient in c?
« Reply #26 on: November 14, 2017, 08:37:13 pm »
If something makes the code more readable and maintainable, i wouldn't care too much about a little loss in efficiency.

Sometimes, function pointers are a neat tool, e. g. for implementing jump tables.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5319
  • Country: gb
Re: Is function in struct efficient in c?
« Reply #27 on: November 14, 2017, 08:59:32 pm »
I might’ve missed it in this thread, but particularly in embedded systems pointers to functions, as well as for callbacks, are reasonably common in state machines as an alternative to large case statements.

Totally agred with the previous poster, it’s whatever makes the most sense in terms of readability, maintenance and efficiency.

 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26906
  • Country: nl
    • NCT Developments
Re: Is function in struct efficient in c?
« Reply #28 on: November 15, 2017, 09:35:39 am »
I might’ve missed it in this thread, but particularly in embedded systems pointers to functions, as well as for callbacks, are reasonably common in state machines as an alternative to large case statements.
But you have to be carefull though. I don't use function pointers if they aren't constant (=fixed in flash) otherwise memory corruption can screw up things badly.  AFAIK MISRA doesn't allow to use function pointers at all (or at least ones that are not in RAM).
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline bd139

  • Super Contributor
  • ***
  • Posts: 23018
  • Country: gb
Re: Is function in struct efficient in c?
« Reply #29 on: November 15, 2017, 09:40:56 am »
I tend to write state machines using macros that wrap a switch block and loop. No pointers, no mallocs. Making it safe and user-readable are the most important things.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5319
  • Country: gb
Re: Is function in struct efficient in c?
« Reply #30 on: November 15, 2017, 12:00:51 pm »
I might’ve missed it in this thread, but particularly in embedded systems pointers to functions, as well as for callbacks, are reasonably common in state machines as an alternative to large case statements.
But you have to be carefull though. I don't use function pointers if they aren't constant (=fixed in flash) otherwise memory corruption can screw up things badly.  AFAIK MISRA doesn't allow to use function pointers at all (or at least ones that are not in RAM).

I was uhming and arhing whether to include  "security" in my list of caveats but didn't want to muddy the waters.

One case I have used mutable function pointers is in an interrupt driven software slave I2C on a very low end device, without either hardware I2C or vectored interrupts, in order to gain sufficiently fast response. It was faster than a bunch of nested if-then-else and case statements, and the difference between it working and not working in this case.

It's just one of the key skills of engineering, compromising and balancing between different criteria and requirements. On the one I cited, it was a very price sensitive application, so much so the cost of a device with hardware I2C would have made it unviable!

In general, the default position for an FSM these day is a case statement, but I don't think it's too much to ask any reasonably proficient C programmer to be able to deal with function pointers put in there for the right reasons.
 

Offline dmills

  • Super Contributor
  • ***
  • Posts: 2093
  • Country: gb
Re: Is function in struct efficient in c?
« Reply #31 on: November 15, 2017, 12:59:04 pm »
Indeed, sometimes you have to do these things (See also Duffs device) and other such cool and fast but overly complicated stuff.

Hackmem is full of such things.

Regards, Dan.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Is function in struct efficient in c?
« Reply #32 on: November 15, 2017, 02:57:24 pm »
in the softcore I am designing some special instructions have been created to simply struct of functions since they are very very very useful  :D
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf