-3
\$\begingroup\$

I'm using keil for working with arm mcu If I put all function that related to each other to a struct , my code will be so easy to read  I do this and in dis assembly code I saw five line for that But when I use the exact function and I see the dis assembly I saw one line I really like to use first method . Do u have any suggestion to work it efficiently?

\$\endgroup\$

closed as unclear what you're asking by efox29, Matt Young, Neil_UK, Lior Bilia, laptop2d Nov 9 '17 at 6:58

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Why not use classes in C++ That is exactly why they exist \$\endgroup\$ – Dirk Bruere Nov 8 '17 at 15:04
  • 2
    \$\begingroup\$ I'm using keil and it does not have c++ \$\endgroup\$ – navid ansari Nov 8 '17 at 15:05
  • \$\begingroup\$ We see nothing that you are talking about. Nor the C code nor the disassembly and until then who knows? \$\endgroup\$ – Bence Kaulics Nov 8 '17 at 15:06
  • 1
    \$\begingroup\$ The code is needed. \$\endgroup\$ – Al Bundy Nov 8 '17 at 15:07
  • 5
    \$\begingroup\$ Please edit your question to include example code. Do not write code in the comments. The question is unclear and will most likely be closed. If you want an answer - provide relevant details and code snippets of the section you are asking about. \$\endgroup\$ – efox29 Nov 8 '17 at 15:09
3
\$\begingroup\$

You are breaking a couple of things by doing this that may or may not matter.

First the linkers ability to drop unused functions goes away because the address of all functions is taken even if they are not eventually called, this may or may not matter to you (depends on how tight you are for code space).

Secondly you are breaking any sort of inter procedural optimisation, again may or may not matter to you.

Thirdly, you are adding extra indirection, which apart from the cost of the extra memory access may or may not do unfortunate things to the cache.

Forthly, by adding these pointers to each structure you are making the structures potentially quite a bit larger, usually better to have a single copy of an auxiliary structure for each type and then have a single pointer from each instance to point to the 'vtable'. Larger matters because your cache is limited, and to at least some extent so is your ram.

For me the big breakage is that automatic documentation extraction tools can no longer easily build calls/called by graphs, which can be a pain when dealing with other peoples code bases, I would far rather have you prefix structure specific function names with something reasonable then do this kind of indirection.

There is a time and a place for this style, IP stacks spring inexorably to mind for some reason.

One suggested improvement if you are going to do this, make the first argument of every function a pointer to the struct in question, that way the code in the function can easily access data from the structure.

C++ really is the way to go here, even if you are really writing 'C with classes' which is usually a better model then full up C++11 for small cores (Turn off exceptions and RTTI, you will hate the memory footprint of that shit). The C compiler will know how to optimise this and will hide a lot of messy details from you.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.