function-pointers

Complex C declaration

二次信任 提交于 2019-11-27 06:11:09
I was just going through some code on the Internet and found this: float * (*(*foo())[SIZE][SIZE])() How do I read this declaration? Is there a specific set of rules for reading such complex declarations? Kos I haven't done this in a while! Start with foo and go right. float * (*(* foo() )[SIZE][SIZE])() foo is a function with no arguments... Can't go right since there's a closing parenthesis. Go left: float * (*( * foo() )[SIZE][SIZE])() foo is a function with no arguments returning a pointer Can't go left further, so let's cross the parentheses and go right again float * (* (* foo()) [SIZE]

C++ allocates abnormally large amout memory for variables

好久不见. 提交于 2019-11-27 06:02:33
问题 I recently got to know an integer takes 4 bytes from the memory. First ran this code, and measured the memory usage: int main() { int *pointer; } It took 144KB. Then I modified the code to allocate 1000 integer variables . int main() { int *pointer; for (int n=0; n < 1000; n++) { pointer = new int ; } } Then it took (168-144=) 24KB but 1000 integers are suppose to occupy (4bytes x 1000=) 3.9KB . Then I decided to make 262,144 integer variables which should consume 1MB of memory . int main() {

c++ Implementing Timed Callback function

假装没事ソ 提交于 2019-11-27 05:29:45
问题 I want to implement some system in c++ so that I can call a function and ask for another function to be called in X milliseconds. Something like this: callfunctiontimed(25, funcName); 25 being the amount of milliseconds before the function should be called. I would like to know if multithreading is required for this and then use some delay function? Other than using function pointer how would a feature like this work? 回答1: Many folks have contributed good answers here on the matter, but I

How does the template parameter of std::function work? (implementation)

人盡茶涼 提交于 2019-11-27 04:59:57
问题 In Bjarne Stroustrup 's home page (C++11 FAQ): struct X { int foo(int); }; std::function<int(X*, int)> f; f = &X::foo; //pointer to member X x; int v = f(&x, 5); //call X::foo() for x with 5 How does it work? How does std::function call a foo member function ? The template parameter is int(X*, int) , is &X::foo converted from the member function pointer to a non-member function pointer ?! (int(*)(X*, int))&X::foo //casting (int(X::*)(int) to (int(*)(X*, int)) To clarify: I know that we don't

C++ Conversion operator for converting to function pointer

社会主义新天地 提交于 2019-11-27 04:29:28
问题 I'm been grinding my head against an idea that is simple enough in my head, but I can't figure out how to implement in C++. Normally, I can declare a class with a conversion operator like in this simple example: class Foo { private: int _i; public: Foo( int i ) : _i(i) { } operator int( ) const { return i; } }; So now I can write awesome stuff like int i = Foo(3); But in my particular case, I would like to provide an operator for converting an object to a function pointer (e.g. converting a

Where are member functions stored for an object?

爱⌒轻易说出口 提交于 2019-11-27 04:26:33
I'm experimenting with C++ to understand how class/structures and their respective objects are laid out in memory and I understood that each field of a class/structure is an offset into their respective object (so I can have a member variable pointer). I don't understand why, even if I can have member function pointers, the following code doesn't work: struct mystruct { void function() { cout << "hello world"; } int c; }; int main() { unsigned int offset_from_start_structure = (unsigned int)(&((mystruct*)0)->c); unsigned int offset_from_start_structure2 = (unsigned int)(&((mystruct*)0)-

convert std::bind to function pointer

丶灬走出姿态 提交于 2019-11-27 03:53:33
I have a third-party library which has a method that takes a function pointer as the first parameter: int third_party_method(void (*func)(double*, double*, int, int, double*), ...); I want to pass a pointer to a class' method that is declared as follows: class TestClass { public: void myFunction (double*, double*, int, int, void*); I tried to pass this function as follows: TestClass* tc = new TestClass(); using namespace std::placeholders; third_party_method(std::bind(&TestClass::myFunction, tc, _1, _2, _3, _4, _5), ...); However, this does not compile: Conversion of parameter 1 from 'std::tr1

Does Function pointer make the program slow?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 03:22:56
I read about function pointers in C. And everyone said that will make my program run slow. Is it true? I made a program to check it. And I got the same results on both cases. (measure the time.) So, is it bad to use function pointer? Thanks in advance. To response for some guys. I said 'run slow' for the time that I have compared on a loop. like this: int end = 1000; int i = 0; while (i < end) { fp = func; fp (); } When you execute this, i got the same time if I execute this. while (i < end) { func (); } So I think that function pointer have no difference of time and it don't make a program

Deducing a function pointer return type

折月煮酒 提交于 2019-11-27 03:22:31
问题 I think code will better illustrate my need: template <typename F> struct return_type { typedef ??? type; }; so that: return_type<int(*)()>::type -> int return_type<void(*)(int,int)>::type -> void I know of decltype and result_of but they need to have arguments passed. I want to deduce the return type of a function pointer from a single template parameter. I cannot add the return type as a parameter, because that's exactly what I want to hide here... I know there's a solution in boost, but I

C++ function types

走远了吗. 提交于 2019-11-27 03:11:15
问题 I have a problem understanding function types (they appear e.g. as the Signature template parameter of a std::function ): typedef int Signature(int); // the signature in question typedef std::function<int(int)> std_fun_1; typedef std::function<Signature> std_fun_2; static_assert(std::is_same<std_fun_1, std_fun_2>::value, "They are the same, cool."); int square(int x) { return x*x; } Signature* pf = square; // pf is a function pointer, easy Signature f; // but what the hell is this? f(42); //