473,763 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof (size_t) and sizeof (pointer)

Does it have to be? :
sizeof (size_t) >= sizeof (pointer)

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Nov 12 '07
16 15661
Bo Persson wrote:
You can only reliably cast the pointer back to the original type. So,
as long as void* is among the largest types, other pointers can be
smaller.
At least in gcc in a 32-bit linux system it seems that a method
pointer is 8 bytes long, while a void* is 4 bytes.

I know this is not related to standard C++ per se, but why does a
method pointer need to be larger than a function pointer? I can't think
of any technical reason for this, because a method cannot be called
through a pointer without an object anyways, so any additional info the
function pointer needs would be in that object, wouldn't it?
Nov 14 '07 #11
On Wed, 14 Nov 2007 14:28:53 +0200, Juha Nieminen wrote:
At least in gcc in a 32-bit linux system it seems that a method
pointer is 8 bytes long, while a void* is 4 bytes.

I know this is not related to standard C++ per se, but why does a
method pointer need to be larger than a function pointer? I can't think
of any technical reason for this, because a method cannot be called
through a pointer without an object anyways, so any additional info the
function pointer needs would be in that object, wouldn't it?
Because a method pointer can point to a virtual function or a non-virtual
function, and when declaring the method pointer, you cannot know where it
will point to.

Say, you have this:

class A;
typedef void (A::*Aptr) ();
Aptr ptrtable[2];

Are the pointers stored in ptrtable virtual or not? You don't know.
You don't even know whether A has virtual functions or not, and thus
whether there is need to express virtual functions. So you need to
be able.

In fact, they may be both, virtual and non-virtual:

class A
{
public:
virtual void afunc() { }
void bfunc() { }
};

int main()
{
ptrtable[0] = &A::afunc;
ptrtable[1] = &A::bfunc;
}

This is valid code.

On the 64-bit and 32-bit Linux systems, GCC and ICC implement method
pointers as a pair of two pointer-size integers, with the following
semantics:

If the first value is even, the second will be zero.
In this case, the first value is a pointer to the member function,
that is not virtual. To follow the pointer, just read the pointer
and jump to that address.
If the first value is odd, this indicates a virtual function.
In this case, the following algorithm will be applied to acquire
the actual function address:
Add the second value to the address of the instance for which
you are calling the method. Read a pointer from that address.
Add the first value, minus 1, to that address. Read a pointer
from that resulting address.
Then jump to the address indicated by that pointer.

With testing I couldn't figure out the situations where the second
value would actually be non-zero, but I trust there are some.
On different platforms, the mechanics behind method pointers can
obviously be different.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Nov 15 '07 #12
Joel Yliluoma wrote:
>
Are the pointers stored in ptrtable virtual or not? You don't know.
You don't even know whether A has virtual functions or not, and thus
whether there is need to express virtual functions. So you need to
be able.
Further, in the case of virtual/multiple inheritance it needs to be able to
have the offset to adjust the "this" pointer as well.

If your compiler is ABSOLUTELY standards compliant, all pointers to
member functions need to be the same size (regardless of whether
there are virtual / multiple inheritance). This is because there
is no "void*" like super poitner for pointer-to-member and someone
made the stupid-assed decision that you should thus be able to
cast between pointer-to-member types and back without losing
information.
Nov 15 '07 #13
On Nov 15, 1:54 pm, Ron Natalie <r...@spamcop.n etwrote:
Joel Yliluoma wrote:
Are the pointers stored in ptrtable virtual or not? You
don't know. You don't even know whether A has virtual
functions or not, and thus whether there is need to express
virtual functions. So you need to be able.
Further, in the case of virtual/multiple inheritance it needs
to be able to have the offset to adjust the "this" pointer as
well.
If your compiler is ABSOLUTELY standards compliant, all
pointers to member functions need to be the same size
(regardless of whether there are virtual / multiple
inheritance). This is because there is no "void*" like super
poitner for pointer-to-member and someone made the
stupid-assed decision that you should thus be able to cast
between pointer-to-member types and back without losing
information.
I don't think that that's the only reason. You can have a
pointer to a member of an incomplete type, so the compiler
cannot possibly know whether there are virtual functions,
mulitple inheritance, etc. or not. VC++ does the optimizations
you refer to, unless you specify otherwise. With the result
that you cannot reliably pass pointer to member functions as
arguments: something like:

class Toto ;

void
f( Toto* p, void (Toto::*f)() )
{
p->*f() ;
}

will not work.

Because you can have pointers to member of an incomplete type,
all pointers to member functions must have the same
representation.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 15 '07 #14
Joel Yliluoma wrote:
On the 64-bit and 32-bit Linux systems, GCC and ICC implement method
pointers as a pair of two pointer-size integers, with the following
semantics:

[ridiculously convoluted semantics omitted]
I don't know why so many compiler writers implement method pointers in such
a complicated way. The easy way to do it is:

* A method pointer is internally just a function pointer (perhaps with a
different calling convention, like fast-this).

* A call x->*p(args...) just does p(x,args...).

* When taking the address of a method, if it can be represented in the
above format, do so; otherwise, generate a proxy function equivalent to

rtntype T::proxy(args) { return method(args); }

and point to that.

This avoids the need for complicated special-case logic for method pointers;
the proxy function always looks the same, and while the code it generates
may be complicated, the logic for generating it is already implemented. What
these other representations amount to is a gratuitous runtime state-machine
implementation of something that could have been compiled to native code
with less implementation effort and probably greater runtime efficiency. Not
to mention that this representation could be easily standardized as part of
an ABI, and is good for implementing delegates.

-- Ben
Nov 28 '07 #15
Ben Rudiak-Gould wrote:
>
* When taking the address of a method, if it can be represented in the
above format, do so; otherwise, generate a proxy function equivalent to

rtntype T::proxy(args) { return method(args); }
How is this any more efficent or less convoluted than storing the method
pointer and a constant to add to the "this" pointer?
Nov 29 '07 #16
Ron Natalie wrote:
How is this any more efficent or less convoluted than storing the method
pointer and a constant to add to the "this" pointer?
If Base::f() is virtual there's no method pointer you can store, because if
Derived overloads f() and x is a Derived, (x.*&Base::f)() calls
Derived::f(). (This is slightly odd given that x.Base::f() calls Base::f().
If they'd given that semantics to member pointers, none of this complexity
would exist.) So we need to encode a second case for virtual functions in
there somehow, and test it at each call site where the call might be virtual
(i.e. where Base is an incomplete type or contains some virtual method
compatible with the method pointer's type).

If f() is non-virtual, but implemented in a virtual base class of Base, then
we have a method pointer but no offset. This would need another case, except
that the standard doesn't require implementations to handle it. (It says
that &Base::f is a BasicBase::*, which isn't compatible with Base::*.)

What if f() is virtual and implemented in a virtual base class? On most
implementations , this is simpler than the previous case. We can handle it
like any other virtual function, because the compiler generated a
pointer-adjusting thunk to put into the vtable -- and it did that so the
vtable could be a vector of function pointers instead of a vector of
pointer-plus-this-adjustment-with-special-case-for-virtual-base thingies.
Vtable entries are method pointers, and they're always, to my knowledge,
implemented in just the way I'm suggesting that surface-language method
pointers should be. Almost all of the necessary code is already in the compiler.

This technique is certainly faster for the trivial case, and almost
certainly faster for the general non-virtual case, since the pointers are
half the size and each call requires an indirect jump and an unconditional
direct jump instead of an indirect jump and a conditional jump (with
potential misprediction). I see two problems with it. One is that it's
almost certainly slower for virtual methods (two indirect jumps), but I
think pointers to virtual methods are much rarer than pointers to
non-virtual methods in the wild. The other is that you can't implement
semantics-preserving casts from Base::* to Derived::* (or vice versa) in
nontrivial cases without horrible convolutions. The only sensible way I can
see to do it is to turn the cast into

switch (p) {
case &Base::f: return &Derived::f;
case &Base::g: return &Derived::g;
// ...
}

which is only workable if you have some way of guaranteeing that you haven't
generated duplicate thunks for the same method. This isn't a fatal problem
since the standard doesn't require such casts to work (unless you cast the
pointer back before using it). It's akin to casting from void (*)(Base*) to
void (*)(Derived*), which would be even harder to implement.

-- Ben
Nov 30 '07 #17

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

Similar topics

13
7693
by: Vinu | last post by:
The following code doesn't prints anything why it is? The code is correct. plz explain the logic #include <stdio.h> int arr = {10,20,30,40,50}; #define ARR_SIZE sizeof(arr)/sizeof(arr) void main()
2
3664
by: blufox | last post by:
I read somewhere that standard Ansi C on 32 bit architecures assumes a lot of things which lead to poor code. For example sizeof( int ) = sizeof( void * ) sizeof( long ) = sizeof( int ) Why is this poor code? Am i missing something?
10
3022
by: Kislay | last post by:
int main() { int i=10; printf("\n Size of i = %d ",sizeof(++i)); printf("\n i = %d ",i); system("pause"); return 0; } On executing the above code , the value of i obtained as 10 . What
20
2185
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then suddenly I can't use sizeof(array) / sizeof(array) anymore within that function ? Help - What point am I missing ?
8
1964
by: Bill Cunningham | last post by:
I would like to check the sizeof size_t. Anyone have a suggestion? Bill
14
2795
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9938
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.