473,513 Members | 6,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is function copy possible?

Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.

Don't consider optimization or performance,
1. Is such an implementation possible?
2. Does such an implementation conform to the ISO C++ standard?

Nov 13 '07 #1
18 2082
WaterWalk wrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.

Don't consider optimization or performance,
1. Is such an implementation possible?
You could be describing JavaScript, where functions can be used as objects.
2. Does such an implementation conform to the ISO C++ standard?
No, for one, how could you take the address of one of these object's
member functions?

--
Ian Collins.
Nov 13 '07 #2
On Nov 13, 6:04 pm, Ian Collins <ian-n...@hotmail.comwrote:
WaterWalk wrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.
Don't consider optimization or performance,
1. Is such an implementation possible?

You could be describing JavaScript, where functions can be used as objects.
2. Does such an implementation conform to the ISO C++ standard?

No, for one, how could you take the address of one of these object's
member functions?
Member function address can still be calculated by adding the class
object's address and the member function offset inside the object.
When calling a function ,just put the function starting address into
the instruction register.

Nov 13 '07 #3
WaterWalk wrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.

Don't consider optimization or performance,
1. Is such an implementation possible?
2. Does such an implementation conform to the ISO C++ standard?
This is possible if you have a function pointer in a class, and you call
it from a class member. Deallocating a class object, you deallocate this
function pointer.

Is this what you had on your mind?
Nov 13 '07 #4
WaterWalk wrote:
Member function address can still be calculated by adding the class
object's address and the member function offset inside the object.
When calling a function ,just put the function starting address into
the instruction register.
LOL
I would like to see someone actually using this.

Can you write an example?
Nov 13 '07 #5
On Nov 13, 6:04 pm, Ian Collins <ian-n...@hotmail.comwrote:
WaterWalk wrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.
Don't consider optimization or performance,
1. Is such an implementation possible?

You could be describing JavaScript, where functions can be used as objects.
Member functions in my imaginary implementation need not to be like
those in JavaScript. I mean, member functions can be copied
implicitly, and thus is opaque to users. Users don't know functions
are copied for each class object.
Nov 13 '07 #6
On Nov 13, 6:42 pm, anon <a...@no.nowrote:
WaterWalk wrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.
Don't consider optimization or performance,
1. Is such an implementation possible?
2. Does such an implementation conform to the ISO C++ standard?

This is possible if you have a function pointer in a class, and you call
it from a class member. Deallocating a class object, you deallocate this
function pointer.

Is this what you had on your mind?
Not exactly. I mean the whole function body is copied, not the
function pointer.

Nov 13 '07 #7
On Nov 13, 6:44 pm, anon <a...@no.nowrote:
WaterWalk wrote:
Member function address can still be calculated by adding the class
object's address and the member function offset inside the object.
When calling a function ,just put the function starting address into
the instruction register.

LOL
I would like to see someone actually using this.

Can you write an example?
This is just an imaginary one. I don't know if it's possible and
conform to the c++ standard.

Nov 13 '07 #8
WaterWalk wrote:
On Nov 13, 6:04 pm, Ian Collins <ian-n...@hotmail.comwrote:
>WaterWalk wrote:
>>Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.
Don't consider optimization or performance,
1. Is such an implementation possible?
You could be describing JavaScript, where functions can be used as objects.

Member functions in my imaginary implementation need not to be like
those in JavaScript. I mean, member functions can be copied
implicitly, and thus is opaque to users. Users don't know functions
are copied for each class object.

Oh sorry, I completely misunderstood you. In my imaginary c++ standard,
thats exactly what happens.
Nov 13 '07 #9
WaterWalk wrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.

Don't consider optimization or performance,
1. Is such an implementation possible?
2. Does such an implementation conform to the ISO C++ standard?
I'm not a C++ language lawyer but this should be possible; you can only
take a pointer to a static member function anyway (one that doesn't need
a hidden pointer to the object). An advantage of your model would be
that one could omit this implicit object pointer for all member function
calls; then one could also take the address of any member function
without any trickery; the huge disadvantage of course would be that
every class instantiation would have to allocate the functions (code) on
the heap; if you only have a few objects and a low instantiation rate,
this might be ok but if you have many objects or a high instantiation
rate, it might quickly become prohibitively expensive.
Some programming language implementations dynamically allocate new
functions on the heap (e.g. if you create new functions at runtime) but
usually not methods for every object instantiation (if such a mechanism
exists) because of the above-mentioned space/time performance.
Nov 13 '07 #10
On 2007-11-13 10:44, WaterWalk wrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.

Don't consider optimization or performance,
1. Is such an implementation possible?
2. Does such an implementation conform to the ISO C++ standard?
I can see no reason why this should not be conformant, the standard does
not tell you how the programs should work, just how they should behave.
Of course there is no practical use for such a scheme since the objects
are not allowed to change the functions in any way, which means that you
would waste a lot of memory on multiple instances of code for the same
function.

--
Erik Wikström
Nov 13 '07 #11
On Nov 13, 10:44 am, WaterWalk <toolmas...@163.comwrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.
Don't consider optimization or performance,
1. Is such an implementation possible?
2. Does such an implementation conform to the ISO C++ standard?
The standard only describes the behavior of the program; it
doesn't say anything about how the implementation obtains that
behavior. Several aspects of the behavior, however, could make
such an implementation difficult, for example, the fact that all
&Class::function must compare equal, regardless of from where
they were obtained.

Pragmatically, such a strategy would increase the size of
objects enormously, for no gain; it would also mean that the
layout of std::complex<floatcould no longer be compatible with
Fortran, which would be a killer for scientific applications.
It's senseless to discuss it much, because it just isn't
reasonable.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 14 '07 #12
On Nov 13, 11:04 am, Ian Collins <ian-n...@hotmail.comwrote:
WaterWalk wrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.
Don't consider optimization or performance,
1. Is such an implementation possible?
You could be describing JavaScript, where functions can be used as objects.
That's true in a lot of interpreted languages, I think. (It's
certianly the case in lisp and the lisp dialects.) But it's not
quite the same thing: in such languages, the "object" which can
be used as a function is either a string with the code for the
function, or a more or less compiled version of the same.
There's still only one instance of each function.
2. Does such an implementation conform to the ISO C++ standard?
No, for one, how could you take the address of one of these
object's member functions?
That may or may not be a problem. All that is required is that
the semantics of pointer to member be respected. And the
pointer to member function can have an arbitrary structure, with
all kinds of data, to allow it to find the correct instance of
the function. It might be more complex to implement (since you
have to guarantee that pointers to the same member function
compare equal), but I'm not convinced that it's not doable.

Another problem might be the fact that POD objects can have
member functions. You can use memcpy to copy POD objects, but
on a machine with absolute addresses, you'd have to relocate the
copy if the function contained any jumps (conditionals or
loops). Even on machines which use relative addresses for jumps
(e.g. Intel), the implementation of dense switches will often
involve a jump table with absolute addresses. (But it's
possible to avoid that as well, and many compilers have a PIC
option for position independent code.)

It would also have interesting implications for "delete this".
(But again, I can think of an implementation that could be made
to work.)

The real question is why anyone would want to do this.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 14 '07 #13
WaterWalk wrote:
Hello. Suppose there is an implementation of C++, in which when a
class object is allocated, its member functions are also allocated in
addition to its data members. So that every class object has a copy
of all of its member functions. When a member function is called, it's
the object's copy that When a class object is deallocated, the
corresponding member functions are also deallocated.
From a technical point of view you might run into problems
implementing that. Many operating systems (such as, AFAIK, linux) do not
allow running data as code by default. Copying the contents of a
function would effectively mean that you are copying the machine code of
the function implementation to a data memory block, after which you
would need to tell the OS that that memory block is ok for execution. In
linux this can be done with a system call (which, naturally, is not very
C++ standard).
This is, in fact, how JIT-compiling works.

OTOH, what would be the point? Unless you are planning in creating
self-modifying code (not a very good idea nowadays), all the functions
would be completely identical. It would just be a waste of memory. (Not
to talk about things like cache inefficiency...)
Nov 14 '07 #14
On Tue, 13 Nov 2007 02:38:00 -0800, WaterWalk wrote:
Member function address can still be calculated by adding the class
object's address and the member function offset inside the object.
No, it cannot.
You seem to have misunderstood something.

When you have a class, say:

class test
{
int a;
void b() { }
int c;
};

And you instantiate the class:
test x;

What happens is NOT that you have an object with layout like this:
00000000-00000003 a
00000004-00000011 b
00000014-00000017 c

Functions are not part of the object's data.

What happens when the class is compiled, is that the code in the
program, will include a function called "test::b()" among all
the other functions, ignoring the question whether they're member
functions or not.
The class instance will only contain memory allocated
for "a" and "c", not for "b" or for a pointer to "b".

For example, say you have this code:
test x;
test y;
std::cout << (void*)&(x.a) << std::endl;
std::cout << (void*)&(x.b) << std::endl;
std::cout << (void*)&(x.c) << std::endl;
std::cout << (void*)&(y.a) << std::endl;
std::cout << (void*)&(y.b) << std::endl;
std::cout << (void*)&(y.c) << std::endl;

Assuming the complain accepted without problem,
you might get this output:
0x13508160
0xE0913500
0x13508164
0x13508170
0xE0913500
0x13508174

Which demonstrates how the function is in the same address for all
instances and separate from where the data of the instance is stored.
(The syntax for member function pointers is different
though, so it won't compile.)
*) Virtual functions are a bit different: each class instance contains
a hidden pointer to a "vtable", which is a constant structure within
the data of the program describing where the virtual functions for
this particular type are located; and the value of the pointer depends
on the type instantiated.
Disclaimer: Implementations may vary for different systems.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Nov 15 '07 #15
On Tue, 13 Nov 2007 02:38:00 -0800, WaterWalk wrote:
Member function address can still be calculated by adding the class
object's address and the member function offset inside the object.
No, it cannot.
You seem to have misunderstood something.

When you have a class, say:

class test
{
int a;
void b() { }
int c;
};

And you instantiate the class:
test x;

What happens is NOT that you have an object with layout like this:
00000000-00000003 a
00000004-00000011 b
00000014-00000017 c

Functions are not part of the object's data.

What happens when the class is compiled, is that the code in the
program, will include a function called "test::b()" among all
the other functions, ignoring the question whether they're member
functions or not.
The class instance will only contain memory allocated
for "a" and "c", not for "b" or for a pointer to "b".

For example, say you have this code:
test x;
test y;
std::cout << (void*)&(x.a) << std::endl;
std::cout << (void*)&(x.b) << std::endl;
std::cout << (void*)&(x.c) << std::endl;
std::cout << (void*)&(y.a) << std::endl;
std::cout << (void*)&(y.b) << std::endl;
std::cout << (void*)&(y.c) << std::endl;

Assuming the compiler accepted the code, you might get this output:
0x13508160
0xE0913500
0x13508164
0x13508170
0xE0913500
0x13508174

Which demonstrates how the function is in the same address for all
instances and separate from where the data of the instance is stored.
(The syntax for member function pointers is different
though, so it won't compile.)
*) Virtual functions are a bit different: each class instance contains
a hidden pointer to a "vtable", which is a constant structure within
the data of the program describing where the virtual functions for
this particular type are located; and the value of the pointer depends
on the type instantiated.
Disclaimer: Implementations may vary for different systems.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Nov 15 '07 #16
Joel Yliluoma wrote:
Functions are not part of the object's data.
Believing that adding a function to a class will increase its size
seems to be a quite common misconception. (No need to split hairs with
adding a virtual function to a class which hadn't any, thanks.)

I think it's also one of the common misconceptions many C++ hating C
hackers have, and consequently one of the flawed arguments against using
C++.

(Quite ironically, many obsessed C++-hating C hackers will emulate
dynamic binding of structs by literally adding function pointers to
structs. Thus each added function will indeed increase the size of the
struct by a function pointer. And then they will be all happy because
they didn't need C++ for that.)
Nov 15 '07 #17
Juha Nieminen wrote:
Believing that adding a function to a class will increase its size
seems to be a quite common misconception. (No need to split hairs with
adding a virtual function to a class which hadn't any, thanks.)
Yeah but the discussion is about (hypothetically) adding a function's
_code_ to each instance, that is, copying the function itself, not a
pointer to it.
And then they will be all happy because
they didn't need C++ for that.)
If that's all that is needed in a certain situation, why not. Keep
things simple.
Nov 16 '07 #18
Matthias Buelow wrote:
Yeah but the discussion is about (hypothetically) adding a function's
_code_ to each instance, that is, copying the function itself, not a
pointer to it.
The original poster seemed to assume in a subsequent post that the
pointer to the function is stored in the object.
>And then they will be all happy because
they didn't need C++ for that.)

If that's all that is needed in a certain situation, why not. Keep
things simple.
Manually adding function pointers to structs (and manually updating
them according to "dynamic bounding") is not something I would call
simple (especially not when compared to C++).
Nov 16 '07 #19

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

Similar topics

1
4015
by: Ronak | last post by:
Hi, I'm encountering a very erratic behavior of STL function random_shuffle() in my code. I have vectors s1 and s2 each having elements {1, 2, 3, 4, 5, 6} which i want to randomly shuffle. My...
6
12648
by: DeepaK K C | last post by:
Could anybody tell me how to pass array to a function by value? -Deepak
0
2160
by: Benjamin Lukner | last post by:
Hi! I'd like to dynamically call API functions from different DLLs, depending on what platform the program runs. Now I'm wondering what the most simple way may be (without meta code and compiler...
12
4028
chunk1978
by: chunk1978 | last post by:
hi there... i'd like to know if it's possible to copy a selected file's name (value?) and insert it into a basic text field thru an onchange event handler... here is my code: <!DOCTYPE html...
3
1595
by: ajayraj | last post by:
Does any body knows why the below mentioned code is not working in VC++ 6.0 compiler. Here a Template function is kept as member and it will work only if the Template member function is defined...
1
1723
by: Visame | last post by:
#include <iostream> using namespace std; class A { public: A() {cout << "constructor A" << endl;} A(A& a) {cout<<"copy A"<<endl;} virtual void Test(){cout <<"A test"...
2
2829
by: bandito40 | last post by:
HI, I am using the copy() function to download a web page as shown in this example: $userAgent = $_SERVER; $context_options = array ( 'http' => array ( 'method' =>...
0
7153
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...
0
7373
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,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7519
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...
1
5079
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...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
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...

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.