Friday 15 June 2012

c++ - Does the address of a function change per runtime -


I am writing something in c ++, where I want to read text from a file that correlates between strings and functions Indicates. I can read the file for example in my program:

  sin: sin pi: getPi +: add   

I want the code to take it and one Create a hash table or vector pointer data structure of string and function. Unfortunately, I realize that the code will not be able to find the name of the function at run-time, so I can put the addresses of these functions in the file. But this system will not have to work, if the work addresses were different each time the program was run or compiled. Solve any of these problems, or alternative solutions would be great.

You can safely take the address of the function (function pointer) on time compilation. When you start your program, this image loader task is to move your function addresses. You do not have to worry about the address change function.

You should definitely not do this, which is printing the address of a function on stdout and places this address in the form of a numeric value in your program. It will suffer from the problem you told.

To get the address of the C ++ function, sin () just enter & amp; Use sin. / P>

To get the address of the C ++ function by the string, you should be like the code:

  if (function_name == "sin") returns and sin; Else if (function_name == "add") return & amp; Add And ...   

Or a suitable data structure with string / function pointer pairs

The issue is: Do not store numeric function addresses in a file. Take the address of the function directly into your program and use the numeric function address should not be any difference to your program.

No comments:

Post a Comment