问题
I tried to get the memory content of the address pointed by the function names [both abc() and main()], but both printf() inside a function give same output, although I dereference the address pointed by the function name in the second printf() inside each function. Why is that? Why the memory addresses are printed by *(abc) and *(main) instead of the contents?
#include<stdio.h>
void abc(){
printf("\n%p \n ", abc);
printf("\n%#x\n ",*(abc));
}
void main(){
abc();
printf("\n%p \n ", main);
printf("\n%#x\n ",*(main));
}
回答1:
You haven't printed the data (code, actually), but the value of the pointer that points at the code.
#include<stdio.h>
void abc(){
int i;
printf("\n%p \n ", abc);
printf("\n%#x\n ",*(abc));
for( i = 0; i < 100; i++ ){
printf( "%X ", ((unsigned char *)abc)[i] );
}
}
void main(){
int i;
printf("\n%p \n ", abc);
abc();
printf("\n%p \n ", main);
printf("\n%#x\n ",*(main));
for( i = 0; i < 100; i++ ){
printf( "%X ", ((unsigned char *)main)[i] );
}
}
This demonstrates that the code is different at the places pointed to by the two function pointers. It is fairly unhelpful unless you're proficient at inverse assembly in your head, but you should be able to verify that the code it dumps agrees with the output of a debugger that disassembles the respective code fragments.
回答2:
You can't dereference a function per se, because in the context of dereferencing it it'll become a function pointer. And, when you dereference it you get the same old function back...
You can try it out like:
printf("one");
(*printf)("two");
(**printf)("three");
To actually peek at the function content you'd need something like memcpy()
. However, doing so is probably not that useful, since there is no way to get the function's size (at least through any standard means). If you want to see what the content of your function looks like, bring your program up in a debugger and disassemble that function.
回答3:
That's because functions and function pointers have really weird syntax.
abc
gives a pointer to function.&abc
also gives a pointer to function.*abc
gives a function designator.
Function designators decay into function pointers, so when you use a function designator in an expression, you get the address of the function.
So abc
, &abc
and *abc
will all give you the same address.
Never assume that C is a rational, sane, consistent language!
回答4:
In C a function pointer does not behave like a regular pointer - it remains a reference to the function whether you use either the dereference or the address-of operator. To achieve your aim, you need to cast it to a different pointer type; for example:
void main()
{
printf("\n%p \n ", main);
printf("\n%#x\n ", *((char*)main) );
}
回答5:
Pointers to functions are not treated the same as pointers to objects, and you can't treat a function pointer as an arbitrary array of bytes like you can with an object pointer.
If you're willing to break a couple of rules, you can try to assign a function pointer to an object pointer, as in
unsigned char *data = (unsigned char *) abc;
and then examine the contents of data[i]
.
Note that your compiler should complain about this. Depending on the platform, this may not work at all. The C standard does not define a conversion from function pointer types to object pointer types or vice versa. Individual implentations are allowed to do this, but it's not required.
来源:https://stackoverflow.com/questions/23785815/why-dereferencing-the-main-function-does-not-show-memory-content