W
William Hughes
jacob said:#include <math.h>
#include <stdio.h>
static int fn(int s)
{
if (s > 0)
return sqrt(s)/pow(s,3);
else return pow(s,3);
}
int main(void)
{
int (*fptr)(int) = fn;
char *p = (char *)fptr;
printf("First 10 bytes of the opcodes of fn are:\n"); for (int
i = 0; i<10;i++) {
printf("[%d] %d 0x%x\n",i,p,p);
}
}
Hello jacob ~~You probably know it, but that code will NOT work on a platform where C
is interpreted not compiled. Also a static function could be completely
removed by the compiler since it is not externally visible, then you
would get the first opcodes of main().Regards ~~
How about a version of C in which you could get and change both source
and object code at all times?
You can get both in .Net, and you can call the compiler which means
you can change both. In principle (I haven't figured out myself how to
do it).
So much for C's "power".
Hmm...taking Herb Schildt's old C interpreter...porting it
to .Net...adding this ability...nyhah ha haaah
Well, there are many implementations where you can access and
modify the executable image. However, a mandate that all
implementations have this capability would put a large burden
on implementations for certain achitechtures (e.g. one in
which the code is in an unreadable segement) which would in
practice mean that this feature of C would not be implemented
for those architectures.
We could (noting that things are very machine specific in any
case) have a function like
get_fuction_opcodes(func, &uint_ptr, &length)
Which would return a pointer to a vector of opcodes, and the
length, or NULL, if this could not be reasonably done, and
a length of -1, if the length was not available.
However, like *any* change to the standard, this has non-trivial
cost, nor is it clear that the functionality produced is really
desirable.
- William Hughes