Some questions about optimization and inline

T

telnet2008

Hi,everyone:

the questionly code:
#include <iostream>

using namespace std;
class test
{
int x;

void func()
{

};
void func1()
{
x = 1;
};

public:
void f()
{
printf("%x\n",&x);
printf("%x\n",&test::func );
printf("%x\n",&test::func1 );
};
};

int main()
{
test a;
a.f();
test * ap = &a;
void(test::*pmem)(void)=&test::f;
printf("%x\n",pmem );
test b;
b.f();
test * bp = &b;
return 0;
}


if delete this 2 statements: printf("%x\n",&test::func );
printf("%x\n",&test::func1 );
in vs2003-release,in export map file and disassemble output file(dumpbin),
cannot find the location of "func()"ºÍ"func1()".

if delete this 3 statements: test * ap = &a;

void(test::*pmem)(void)=&test::f;
printf("%x\n",pmem );
test b;
in vs2003-release,in export map file and disassemble output file, cannot
find the location of "f()".

why?

if I define test::f() out of the class test, like this:

class test
{
int x;

void func()
{

};
void func1()
{
x = 1;
};

public:
void f();
};

void test::f()
{
printf("%x\n",&x);
//printf("%x\n",&test::func );
//printf("%x\n",&test::func1 );
};



I also cannot find the location of "f()" in the output disassemble file
,why?
 
T

Tamas Demjen

telnet2008 said:
printf("%x\n",&x);
printf("%x\n",&test::func );
printf("%x\n",&test::func1 );
};

This is a bad idea. If you want to print the address of a non-member
function or variable, you have to use the %p printf format specifier,
not %x. On Win64 a pointer is 64-bit, but %x assumes you're passing a
32-bit integer.

Note that non-static member function pointers are not simple addresses
and they will fail even with %p. A member function pointer is at least
twice as large as a void*, but it could be several times larger than
that (even as much as 20 bytes per pointer). To make things worse, this
is implementation dependent, so you have no reliable way of knowing how
long they are. I would recommend against using printf with pointers to
member functions. If you must know the address of it, you should use
sizeof(&test::func), and convert that many bytes into hex.
in vs2003-release,in export map file and disassemble output file(dumpbin),
cannot find the location of "func()"ºÍ"func1()".

It looks like you never actually call that function, so the linker
decides not to link it to your EXE.

Tom
 
N

Nathan Mates

This is a bad idea. If you want to print the address of a non-member
function or variable, you have to use the %p printf format specifier,
not %x. On Win64 a pointer is 64-bit, but %x assumes you're passing a
32-bit integer.

Does %P work to print things in CAPSHex, e.g. 0xDEADBEEF, not
0xdeadbeef? Lowercase hex just looks *wrong* to me.

Nathan Mates
 
T

Tamas Demjen

Nathan said:
Does %P work to print things in CAPSHex, e.g. 0xDEADBEEF, not
0xdeadbeef? Lowercase hex just looks *wrong* to me.

%p prints in uppercase for me. There is no such thing as %P on my
version. Note that %p is not 100% portable -- many things about printf
are not very portable. For example, gcc uses %lld for 64-bit integers,
Microsoft uses %I64d, Borland uses %Ld (although it also understands %I64d).

Once again, printf doesn't have anything for non-static member function
pointers. If the function doesn't use virtual inheritance, you could try
%p%p, just double check that sizeof(&C::f) is indeed sizeof(void*) * 2.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top