473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Casting pointers to functions of different types

Hey,

Is the following code valid? It has to cast a function pointer to some
fixed type to allow api for storing and calling that function, but is
such cast legal? Code which stored function pointer in array of unsigned
chars would be valid (I guess), but would be lot uglier than this.

typedef void (*AFunc) (void);
typedef void (*ConcreteFunc) (int);

void some_func (int)
{
}

void marshal (AFunc func, int *args)
{
ConcreteFunc cfunc = func;
cfunc (args[0]);
}

int main (void)
{
int arg = 1;
AFunc func = some_func; // here we cast ConcreteFunc to AFunc
marshal (func, &arg, 1); // and then marshal() casts it back
}

Thanks,
Yevgen
Oct 6 '06 #1
5 1654
Yevgen Muntyan wrote:
Hey,

Is the following code valid?
Here's compilable code, sorry:

typedef void (*AFunc) (void);
typedef void (*ConcreteFunc) (int);

void some_func (int arg)
{
}

void marshal (AFunc func, int *args)
{
ConcreteFunc cfunc = (ConcreteFunc) func;
cfunc (args[0]);
}

int main (void)
{
int arg = 1;
AFunc func = (AFunc) some_func;// here we cast ConcreteFunc to AFunc
marshal (func, &arg); // and then marshal() casts it back
}
Oct 6 '06 #2
I never think this can work, you'd better check on your compiler,but I
think 99% chance, you will receive an error message from the compiler.
and if it is C++, you must wrong.
If your compiler is old version, you may test a function with no
parameter, that means, it will not check the parameter, maybe it can
work. Good Luck!
Yevgen Muntyan wrote:
Hey,

Is the following code valid? It has to cast a function pointer to some
fixed type to allow api for storing and calling that function, but is
such cast legal? Code which stored function pointer in array of unsigned
chars would be valid (I guess), but would be lot uglier than this.

typedef void (*AFunc) (void);
typedef void (*ConcreteFunc) (int);

void some_func (int)
{
}

void marshal (AFunc func, int *args)
{
ConcreteFunc cfunc = func;
cfunc (args[0]);
}

int main (void)
{
int arg = 1;
AFunc func = some_func; // here we cast ConcreteFunc to AFunc
marshal (func, &arg, 1); // and then marshal() casts it back
}

Thanks,
Yevgen
Oct 6 '06 #3
Yevgen Muntyan wrote:
Hey,

Is the following code valid?
Nevermind, standard explicitely states it is, except errors
not related to casts :)

"A pointer to a function of one type may be converted to a pointer to a
function of another type and back again; the result shall compare equal
to the original pointer."

Regards,
Yevgen
Oct 6 '06 #4
Yevgen Muntyan said:
Hey,

Is the following code valid?
No comment! :-) I'm going to answer the question, if I may, rather than the
code.
It has to cast a function pointer to some
fixed type to allow api for storing and calling that function, but is
such cast legal?
Yes. All function pointer types are guaranteed to have the same
representation, so it's okay to store any function pointer value in any
function pointer object. BUT there are no implicit conversions, like with
void * for objects. So unless you know the RHS is exactly the proper type
for the LHS, you must add a cast to get it to the right type. This is
another of those very few places where a cast is necessary and therefore
worth having.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 6 '06 #5
On Fri, 06 Oct 2006 04:26:51 +0000, Richard Heathfield
<in*****@invalid.invalidwrote:
Yevgen Muntyan said:
It has to cast a function pointer to some
fixed type to allow api for storing and calling that function, but is
such cast legal?

Yes. All function pointer types are guaranteed to have the same
representation, so it's okay to store any function pointer value in any
function pointer object. <snip>
They aren't specifically required to have the same representation. It
is required that you can convert any function pointer type to any
other and back without loss of information, and I have never heard of
or even imagined any machine where it would make sense to accomplish
this by any means other than using the same representation.

But what the OP was doing (or trying to) was converting, and that is
specifically guaranteed.

- David.Thompson1 at worldnet.att.net
Oct 23 '06 #6

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
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:
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
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...
1
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...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.