473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Some questions about optimization and inline

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?

Mar 12 '07 #1
3 1246
telnet2008 wrote:
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
Mar 12 '07 #2
In article <ud**************@TK2MSFTNGP04.phx.gbl>,
Tamas Demjen <td*****@yahoo.comwrote:
>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

--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Mar 12 '07 #3
Nathan Mates wrote:
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
Mar 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: cai | last post by:
Hello everyone, I am reading Inside the c++ object model(Lippman). In section 2.3 of Chapter 2, I am puzzled by an example. class test { friend test foo(double); public: test()...
1
by: Timothy Stark | last post by:
Hello folks, Does anyone have any information about good books about C/C++ optiimization? I am developing my emulator by using C++ language. I compared generated assembly sources between two...
6
by: puzzlecracker | last post by:
I have recently decided to go over the questions in the classical book by Murray and few of them admittedly befuddled me. page 140: 1. Under what circumstances should a smart compiler be able...
50
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
22
by: NigelW | last post by:
This is really a question for the development team. Are there plans to improve the optimization of C# to MSIL? I ask this, as inspection with ILDASM of the MSIL code shows that, even with the...
7
by: Jaimi McEntire | last post by:
Given a property define like this: public decimal ControlAmount { get { return mControlAmount; } set { mControlAmount = value; } } I expected (under release mode) that when used as a...
66
by: KimmoA | last post by:
Hey! Some questions about C that have been bugging me for a while... 1) Is inline a valid C keyword or not? I was kind of surprised of not finding it in C, to be honest. My "The C Programming...
5
by: Joel | last post by:
(1) Can anyone please tell me the exact meaning of primitive types in the MSIL context. (2) Also what is the meaning of the world inline? (3) What is the meaning of the statement: "You should...
6
by: wqe | last post by:
Hi,everyone: the questionly code: #include <iostream> using namespace std; class test { int x;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.