Problem: I have to parse the payload of a packet. The payload could be in Big Endian Format (network byte order) or little. That depends on a flag present in the header of the packet.
Solution: A horrible solution could be to check for that flag everytime I have to read a field in the payload, that is
and that would be done in all the function body that is responsible for parsing the payload. Quite awful indeed.
I was thinking to use a function pointer, actually two, and init them to ntohl and ntohs if the payload was in network order, or init them to an empty function otherwise. Something like
Now. Before going ahead and mess up with my already-working-code-without-byte-ordering-support, I'd like to know if this solution is
1 - correct
2 - efficient
or if any of you guys have already faced this problem and have a better and more elegant solution.
In the subject, I wrote function pointer to inline function, because I was wondering if the empty function could be inline to improve performances and avoid the function call
overhead.
Would the pointer function call be replaced with actual inline function in case it points to the inline one?
The answer if of course not, because the last one is done at
compile time. Yeah.. I'm basically answering myself while writing. :-)
Is there some reason that the application generating the payload sometimes does it in network byte order and sometimes LE order? If you have control over this application, I suggest you fix it up to always generate network byte order packets.
Is there some reason that the application generating the payload sometimes does it in network byte order and sometimes LE order?
Yes.
Quote:
Originally Posted by fpmurphy
If you have control over this application, I suggest you fix it up to always generate network byte order packets.
Too easy! :-)
No, I don't have any control over it. Even because the packet is not always sent through actual IP protocol, but it could also be sent through USB.
In the subject, I wrote function pointer to inline function, because I was wondering if the empty function could be inline to improve performances and avoid the function call overhead.
Are you typing up these responses in another program then copy-pasting? You don't need to do that, it adds pointless extra linebreaks. The text will wrap by itself when it needs to.
The performance difference between a function pointer and if/else is not going to be significant here, use whatever is clearest. If it was a choice between three or more functions, a function pointer might be more efficient and elegant.
Although it's four functions and not only two. Thus means
two arrays of pointers.
Interesting solution anyway.
S.
---------- Post updated at 01:48 AM ---------- Previous update was at 01:46 AM ----------
Quote:
Originally Posted by Corona688
Are you typing up these responses in another program then copy-pasting? You don't need to do that, it adds pointless extra linebreaks. The text will wrap by itself when it needs to.
I'm not copying-and-pasting anything.
Quote:
Originally Posted by Corona688
The performance difference between a function pointer and if/else is not going to be significant here, use whatever is clearest. If it was a choice between three or more functions, a function pointer might be more efficient and elegant.
The point is that the function is called _VERY_ often, so the more is optimized, the better.
Corona is correct - you should leave optimization to compilers when complex makes code harder to read and maintain. And the speed return is minimal.
General tips:
If you have already not done so - try profiling your code.
Algorithm changes usually provide far better optimization than tweaks like function pointers. This is the reason for seemingly odd algorithms like Duff's machine. And threading.
Turn on optimization for your compiler - things like loop unrolling may provide a lot of speed increase.
Hi All,
Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition.
#Body
function1()
{
if
then
echo "exist"
else
echo "not exist"
}
#if not exist in function1 my all other function will not proceed.... (4 Replies)
I am passing a char* to the function "reverse" and when I execute it with gdb I get:
Program received signal SIGSEGV, Segmentation fault.
0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72
72 *q = *p;
Attached is the source code.
I do not understand why... (9 Replies)
Have difficulty to understand this pure C code to only print vowels twice from input string. Questions are commented at the end of each place.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
/*
*Demonstrate the use of dispatch tables
*/
/*Print a char... (11 Replies)
Hi,
In the below C code,
#include <stdio.h>
void print() {
printf("Hello\n");
}
int main() {
void (*f)() = (void (*)()) print;
f();
(*f)();
}
I wonder, how the syntaxes "f()" and "(*f)()" are treated as same without any error? Is this an improvement or ANSI/ISO... (1 Reply)
if i create an array of pointers to a structure "struct node" as:
struct node *r;
and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as:
r=multiplty(.......) /*some parameters*/
is... (2 Replies)
#include <iostream>
using namespace std;
class A
{
public:
int Getvalue() { return i;}
private:
int i;
};
int main()
{}
The above code compiles properly in g++ or in any other C++ compiler.
BUT, the variable 'i' is used (in 'return i' statement) before it is... (1 Reply)
Hello all
im trying to build function that will return void function pointer
what is mean is ( not working )
the main function
void * myClass::getFunction(int type){
if(type==1)
return &myClass::Test1;
if(type==2)
return &myClass::Test2;
}
void myClass::Test1(){... (1 Reply)
i have a function:
char *pcCityIdToCountryName(ADMIN_DB_DATA *pstHEader, unit uiCityID)
this returns a pointer to CountryName if cityId is given.
to retrieve countryname i give:
char *CountryName;
CountryName = pcCityIdToCountryName(..................);
but when i compile it is giving :... (5 Replies)
using namespace std;
void g();
class A {
public :
A() { g();g();g(); cout << "Constructor of A"<< endl ;}
};
inline void g(){ cout << "vijay" <<endl; }
int main() {
A a;
}
when i use inline i get size 303488 Aug 31 12:05 a.out*
when not using inline i get size 303572 Aug 31... (1 Reply)